Skip to content

Commit

Permalink
Simplify logic for disable-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Orvell committed Jul 12, 2019
1 parent 9756d86 commit 9c6f266
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions lib/legacy/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,24 @@ function GenerateClassFromInfo(info, Base, behaviors) {
return observedAttributesGetter.call(this).concat(DISABLED_ATTR);
}

// Prevent element from initializing properties when it's upgrade disabled.
/** @override */
_initializeProperties() {
if (!this.hasAttribute(DISABLED_ATTR)) {
super._initializeProperties();
} else {
this.__isUpgradeDisabled = true;
}
}

// Prevent element from enabling properties when it's upgrade disabled.
/** @override */
_enableProperties() {
if (!this.__isUpgradeDisabled) {
super._enableProperties();
}
}

/**
* @override
* @param {string} name Attribute name.
Expand All @@ -248,48 +266,33 @@ function GenerateClassFromInfo(info, Base, behaviors) {
*/
attributeChangedCallback(name, old, value, namespace) {
if (name == DISABLED_ATTR) {
if (!this.__dataEnabled && value == null && this.isConnected) {
super.connectedCallback();
// When disable-upgrade is removed, intialize properties and
// provoke connectedCallback if the element is already connected.
if (!this.__dataEnabled && value == null) {
super._initializeProperties();
this.__isUpgradeDisabled = false;
if (this.isConnected) {
super.connectedCallback();
}
}
} else {
super.attributeChangedCallback(
name, old, value, /** @type {null|string} */ (namespace));
}
}


// prevent user code in connected from running
// Prevent element from connecting when it's upgrade disabled.
/** @override */
connectedCallback() {
if (this.__dataEnabled || !this.hasAttribute(DISABLED_ATTR)) {
if (!this.__isUpgradeDisabled) {
super.connectedCallback();
}
}

_initializeProperties() {
if (!this.hasAttribute(DISABLED_ATTR)) {
super._initializeProperties();
} else {
this.__wasDisabled = true;
}
}

// prevent element from turning on properties
/** @override */
_enableProperties() {
if (this.__wasDisabled) {
this.__wasDisabled = false;
super._initializeProperties();
}
if (!this.hasAttribute(DISABLED_ATTR)) {
super._enableProperties();
}
}

// only go if "enabled"
// Prevent element from disconnecting when it's upgrade disabled.
/** @override */
disconnectedCallback() {
if (this.__dataEnabled) {
if (!this.__isUpgradeDisabled) {
super.disconnectedCallback();
}
}
Expand Down

0 comments on commit 9c6f266

Please sign in to comment.