Skip to content

Commit

Permalink
Fix issue with observers being called twice
Browse files Browse the repository at this point in the history
  • Loading branch information
TimvdLippe committed Jan 27, 2018
1 parent cb88252 commit 291e4f5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
13 changes: 9 additions & 4 deletions lib/mixins/properties-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
*/
function superPropertiesClass(constructor) {
const superCtor = Object.getPrototypeOf(constructor);

// Note, the `PropertiesMixin` class below only refers to the class
// generated by this call to the mixin; the instanceof test only works
// because the mixin is deduped and guaranteed only to apply once, hence
Expand All @@ -87,10 +88,14 @@
* @return {Object} Memoized properties object
*/
function ownProperties(constructor) {
if (!constructor.hasOwnProperty(
JSCompiler_renameProperty('__ownProperties', constructor))) {
const props = constructor.properties;
constructor.__ownProperties = props ? normalizeProperties(props) : null;
if (!constructor.hasOwnProperty(JSCompiler_renameProperty('__ownProperties', constructor))) {
let props = null;

if (constructor.hasOwnProperty('properties') && constructor.properties) {
props = normalizeProperties(constructor.properties);
}

constructor.__ownProperties = props;
}
return constructor.__ownProperties;
}
Expand Down
25 changes: 24 additions & 1 deletion test/unit/property-effects-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -1038,4 +1038,27 @@
this.xChanged = sinon.spy();
}
});
</script>
</script>

<script>
class SuperObserverElement extends Polymer.Element {
static get is() { return 'super-observer-element'; }
static get properties() {
return {
prop: {
value: 'String',
observer(value, oldValue) {
this.__observerCalled++;
}
}
}
}
}
SuperObserverElement.prototype.__observerCalled = 0;
customElements.define(SuperObserverElement.is, SuperObserverElement);

class SubObserverElement extends SuperObserverElement {
static get is() { return 'sub-observer-element'; }
}
customElements.define(SubObserverElement.is, SubObserverElement);
</script>
10 changes: 10 additions & 0 deletions test/unit/property-effects.html
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,16 @@
}
});

suite('observer inheritance', function() {
setup(function() {
el = document.createElement('sub-observer-element');
document.body.appendChild(el);
});

test('does not invoke observer twice', function() {
assert.equal(el.__observerCalled, 1);
});
});
});

suite('computed bindings with dynamic functions', function() {
Expand Down

0 comments on commit 291e4f5

Please sign in to comment.