Skip to content

Commit

Permalink
Use DisbleUpgradeMixin in legacy class generation
Browse files Browse the repository at this point in the history
This was duplicated for performance but the benefit tested as marginal
  • Loading branch information
Steven Orvell committed Jul 18, 2019
1 parent f494389 commit 2203dae
Showing 1 changed file with 4 additions and 93 deletions.
97 changes: 4 additions & 93 deletions lib/legacy/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN

import { LegacyElementMixin } from './legacy-element-mixin.js';
import { legacyOptimizations } from '../utils/settings.js';
import { wrap } from '../utils/wrap.js';
import { DisableUpgradeMixin } from '../mixins/disable-upgrade-mixin.js';

const lifecycleProps = {
attached: true,
Expand Down Expand Up @@ -176,10 +176,6 @@ function mergeProperties(target, source) {
}
}

const DISABLED_ATTR = 'disable-upgrade';

let observedAttributesGetter;

/* Note about construction and extension of legacy classes.
[Changed in Q4 2018 to optimize performance.]
Expand Down Expand Up @@ -216,97 +212,9 @@ function GenerateClassFromInfo(info, Base, behaviors) {
let behaviorList;
const lifecycle = {};

// Work around for closure bug #126934458. Using `super` in a property
// getter does not work so instead we search the Base prototype for an
// implementation of observedAttributes so that we can override and call
// the `super` getter. Note, this is done one time ever because we assume
// that `Base` is always comes from `Polymer.LegacyElementMixn`.
if (!observedAttributesGetter) {
let ctor = Base;
while (ctor && !observedAttributesGetter) {
const desc = Object.getOwnPropertyDescriptor(ctor, 'observedAttributes');
if (desc) {
observedAttributesGetter = desc.get;
}
ctor = Object.getPrototypeOf(ctor.prototype).constructor;
}
}

/** @private */
class PolymerGenerated extends Base {

// NOTE, this copies most of the code from DisableUpgradeMixin. This is
// unfortunate, but testing revealed a small penalty to applying the mixin
// on top of every generated class, so we do it directly here.
static get observedAttributes() {
return observedAttributesGetter.call(this).concat(DISABLED_ATTR);
}

// Prevent element from initializing properties when it's upgrade disabled.
/** @override */
_initializeProperties() {
// Enable disable-upgrade use when `legacyOptimizations` setting is on.
if (legacyOptimizations && this.hasAttribute(DISABLED_ATTR)) {
this.__isUpgradeDisabled = true;
} else {
super._initializeProperties();
}
}

// Prevent element from enabling properties when it's upgrade disabled.
// Normally overriding connectedCallback would be enough, but dom-* elements
// enable elements early.
/** @override */
_enableProperties() {
if (!this.__isUpgradeDisabled) {
super._enableProperties();
}
}

/**
* @override
* @param {string} name Attribute name.
* @param {?string} old The previous value for the attribute.
* @param {?string} value The new value for the attribute.
* @param {?string} namespace The XML namespace for the attribute.
* @return {void}
*/
attributeChangedCallback(name, old, value, namespace) {
if (name == DISABLED_ATTR) {
// When disable-upgrade is removed, intialize properties and
// provoke connectedCallback if the element is already connected.
if (this.__isUpgradeDisabled && value == null) {
super._initializeProperties();
this.__isUpgradeDisabled = false;
if (wrap(this).isConnected) {
super.connectedCallback();
}
}
} else {
super.attributeChangedCallback(
name, old, value, /** @type {null|string} */ (namespace));
}
}

// Prevent element from connecting when it's upgrade disabled.
// This prevents user code in `attached` from being called.
/** @override */
connectedCallback() {
if (!this.__isUpgradeDisabled) {
super.connectedCallback();
}
}

// Prevent element from disconnecting when it's upgrade disabled.
// This avoids allowing user code `detached` from being called without a
// paired call to `attached`.
/** @override */
disconnectedCallback() {
if (!this.__isUpgradeDisabled) {
super.disconnectedCallback();
}
}

// explicitly not calling super._finalizeClass
/** @nocollapse */
static _finalizeClass() {
Expand Down Expand Up @@ -626,6 +534,9 @@ export const Class = function(info, mixin) {
let klass = mixin ? mixin(LegacyElementMixin(HTMLElement)) :
LegacyElementMixin(HTMLElement);
klass = GenerateClassFromInfo(info, klass, info.behaviors);
if (legacyOptimizations) {
klass = DisableUpgradeMixin(klass);
}
// decorate klass with registration info
klass.is = klass.prototype.is = info.is;
return klass;
Expand Down

0 comments on commit 2203dae

Please sign in to comment.