Skip to content

Commit e0ba67c

Browse files
author
Steven Orvell
committed
Update stand alone disable-upgrade mixin.
1 parent 872094a commit e0ba67c

File tree

1 file changed

+49
-29
lines changed

1 file changed

+49
-29
lines changed

lib/mixins/disable-upgrade-mixin.js

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { dedupingMixin } from '../utils/mixin.js';
1515

1616
const DISABLED_ATTR = 'disable-upgrade';
1717

18+
let observedAttributesGetter;
19+
1820
/**
1921
* Element class mixin that allows the element to boot up in a non-enabled
2022
* state when the `disable-upgrade` attribute is present. This mixin is
@@ -51,18 +53,49 @@ export const DisableUpgradeMixin = dedupingMixin((base) => {
5153
*/
5254
const superClass = ElementMixin(base);
5355

56+
// Work around for closure bug #126934458. Using `super` in a property
57+
// getter does not work so instead we search the Base prototype for an
58+
// implementation of observedAttributes so that we can override and call
59+
// the `super` getter. Note, this is done one time ever because we assume
60+
// that `Base` is always comes from `Polymer.LegacyElementMixn`.
61+
if (!observedAttributesGetter) {
62+
let ctor = superClass;
63+
while (ctor && !observedAttributesGetter) {
64+
const desc = Object.getOwnPropertyDescriptor(ctor, 'observedAttributes');
65+
if (desc) {
66+
observedAttributesGetter = desc.get;
67+
}
68+
ctor = Object.getPrototypeOf(ctor.prototype).constructor;
69+
}
70+
}
71+
5472
/**
5573
* @polymer
5674
* @mixinClass
5775
* @implements {Polymer_DisableUpgradeMixin}
5876
*/
5977
class DisableUpgradeClass extends superClass {
6078

61-
/**
62-
* @suppress {missingProperties} go/missingfnprops
63-
*/
6479
static get observedAttributes() {
65-
return super.observedAttributes.concat(DISABLED_ATTR);
80+
return observedAttributesGetter.call(this).concat(DISABLED_ATTR);
81+
}
82+
83+
// Prevent element from initializing properties when it's upgrade disabled.
84+
/** @override */
85+
_initializeProperties() {
86+
if (!this.hasAttribute(DISABLED_ATTR)) {
87+
super._initializeProperties();
88+
} else {
89+
this.__isUpgradeDisabled = true;
90+
}
91+
}
92+
93+
// Prevent element from enabling properties when it's upgrade disabled.
94+
/** @override */
95+
_enableProperties() {
96+
if (!this.__isUpgradeDisabled) {
97+
super._enableProperties();
98+
}
6699
}
67100

68101
/**
@@ -75,46 +108,33 @@ export const DisableUpgradeMixin = dedupingMixin((base) => {
75108
*/
76109
attributeChangedCallback(name, old, value, namespace) {
77110
if (name == DISABLED_ATTR) {
78-
if (!this.__dataEnabled && value == null && this.isConnected) {
79-
super.connectedCallback();
111+
// When disable-upgrade is removed, intialize properties and
112+
// provoke connectedCallback if the element is already connected.
113+
if (!this.__dataEnabled && value == null) {
114+
super._initializeProperties();
115+
this.__isUpgradeDisabled = false;
116+
if (this.isConnected) {
117+
super.connectedCallback();
118+
}
80119
}
81120
} else {
82121
super.attributeChangedCallback(
83122
name, old, value, /** @type {null|string} */ (namespace));
84123
}
85124
}
86125

87-
/*
88-
NOTE: cannot gate on attribute because this is called before
89-
attributes are delivered. Therefore, we stub this out and
90-
call `super._initializeProperties()` manually.
91-
*/
92-
/** @override */
93-
_initializeProperties() {}
94-
95-
// prevent user code in connected from running
126+
// Prevent element from connecting when it's upgrade disabled.
96127
/** @override */
97128
connectedCallback() {
98-
if (this.__dataEnabled || !this.hasAttribute(DISABLED_ATTR)) {
129+
if (!this.__isUpgradeDisabled) {
99130
super.connectedCallback();
100131
}
101132
}
102133

103-
// prevent element from turning on properties
104-
/** @override */
105-
_enableProperties() {
106-
if (!this.hasAttribute(DISABLED_ATTR)) {
107-
if (!this.__dataEnabled) {
108-
super._initializeProperties();
109-
}
110-
super._enableProperties();
111-
}
112-
}
113-
114-
// only go if "enabled"
134+
// Prevent element from disconnecting when it's upgrade disabled.
115135
/** @override */
116136
disconnectedCallback() {
117-
if (this.__dataEnabled) {
137+
if (!this.__isUpgradeDisabled) {
118138
super.disconnectedCallback();
119139
}
120140
}

0 commit comments

Comments
 (0)