Slightly improve how _registered is called.

This ensures it's not called on extendors who do not specifically implement `_registered`. This is important since it's expected to do prototype specific work.
This commit is contained in:
Steven Orvell
2018-11-06 12:58:44 -08:00
parent f62755d4b5
commit 5d5c95caec
2 changed files with 21 additions and 28 deletions
+15 -27
View File
@@ -205,12 +205,8 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
are controlled via the finalization mechanism provided by `Properties-Mixin`.
`Properties` and `observers` are collected by manually traversing the prototype
chain and merging.
The `_registered` method is called via `LegacyElementMixin._finalizeClass`
and is called on each prototype in the element's chain. Because a non-legacy
element may extend a legacy one, it's important that work in `_registered`
carefully act only once.
chain and merging. The `_registered` method is called via
`LegacyElementMixin._finalizeClass`.
*/
/**
@@ -224,7 +220,6 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
function GenerateClassFromInfo(info, Base, behaviors) {
// manages behavior and lifecycle processing (filled in after class definition)
let registered = false;
let activeBehaviors;
const lifecycle = {};
@@ -285,27 +280,20 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
`is` in `beforeRegister` as you could in 1.x.
*/
const proto = this;
// NOTE: this `registered` flag is required so that extensions
// that do not override `_registered` do not try to "re-register"
// this data. Only extensions that use `mixinBehaviors` will normally
// have this implementation.
if (!registered) {
registered = true;
if (activeBehaviors) {
copyAndFilterBehaviors(proto, activeBehaviors, lifecycle);
if (activeBehaviors) {
copyAndFilterBehaviors(proto, activeBehaviors, lifecycle);
}
copyAndFilterProperties(proto, info, lifecycle);
let list = lifecycle.beforeRegister;
if (list) {
for (let i=0; i < list.length; i++) {
list[i].call(proto);
}
copyAndFilterProperties(proto, info, lifecycle);
let list = lifecycle.beforeRegister;
if (list) {
for (let i=0; i < list.length; i++) {
list[i].call(proto);
}
}
list = lifecycle.registered;
if (list) {
for (let i=0; i < list.length; i++) {
list[i].call(proto);
}
}
list = lifecycle.registered;
if (list) {
for (let i=0; i < list.length; i++) {
list[i].call(proto);
}
}
}
+6 -1
View File
@@ -95,7 +95,12 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
}
static _finalizeClass() {
this.prototype._registered();
// Note, call `_registered` only if this specific prototype has
// an implementation; this ensures `_registered` is not called
// on extenders that do not implement it.
if (this.prototype.hasOwnProperty('_registered')) {
this.prototype._registered();
}
super._finalizeClass();
}