Skip to content

Commit c1885a6

Browse files
author
Steven Orvell
committed
[PropertiesChanged]: adds _shouldPropertiesChange
Adds `_shouldPropertiesChange` method which can be overridden to customize when the `_propertiesChanged` method is called. This method gets the current, pending, and old property values and by default returns truthy...
1 parent 9b3ca2c commit c1885a6

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

lib/mixins/properties-changed.html

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
/* eslint-disable valid-jsdoc */
144144
/** @this {PropertiesChanged} */
145145
get() {
146-
return this.__data[property];
146+
return this._getProperty(property);
147147
},
148148
/** @this {PropertiesChanged} */
149149
set: readOnly ? function () {} : function (value) {
@@ -332,13 +332,31 @@
332332
* @protected
333333
*/
334334
_flushProperties() {
335-
if (this.__dataPending && this.__dataOld) {
336-
let changedProps = this.__dataPending;
335+
const props = this.__data;
336+
const changedProps = this.__dataPending;
337+
const old = this.__dataOld;
338+
if (this._shouldPropertiesChange(props, changedProps, old)) {
337339
this.__dataPending = null;
338-
this._propertiesChanged(this.__data, changedProps, this.__dataOld);
340+
this._propertiesChanged(props, changedProps, old);
339341
}
340342
}
341343

344+
/**
345+
* Called in `_flushProperties` to determine if `_propertiesChanged`
346+
* should be called. The default implementation returns true if
347+
* properties are pending. Override to customize when
348+
* `_propertiesChanged` is called.
349+
* @param {!Object} currentProps Bag of all current accessor values
350+
* @param {!Object} changedProps Bag of properties changed since the last
351+
* call to `_propertiesChanged`
352+
* @param {!Object} oldProps Bag of previous values for each property
353+
* in `changedProps`
354+
* @return {boolean} true if changedProps is truthy
355+
*/
356+
_shouldPropertiesChange(props, changedProps, old) {
357+
return changedProps;
358+
}
359+
342360
/**
343361
* Callback called when any properties with accessors created via
344362
* `_createPropertyAccessor` have been set.

test/unit/properties-changed.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@
5656
}
5757
window.XFoo = XFoo;
5858
customElements.define('x-foo', XFoo);
59+
60+
class ShouldPropertiesChange extends Polymer.PropertiesChanged(HTMLElement) {
61+
constructor() {
62+
super();
63+
this._propertiesChanged = sinon.spy();
64+
}
65+
66+
connectedCallback() {
67+
this._enableProperties();
68+
//this._flushProperties();
69+
}
70+
71+
_shouldPropertiesChange() {
72+
return true;
73+
}
74+
}
75+
76+
customElements.define('should-properties-change', ShouldPropertiesChange);
5977
});
6078
</script>
6179

@@ -129,6 +147,21 @@
129147
});
130148
});
131149

150+
test('shouldPropertiesChange', function(done) {
151+
const el = document.createElement('should-properties-change');
152+
document.body.appendChild(el);
153+
assert.isTrue(el._propertiesChanged.calledOnce);
154+
el._invalidateProperties();
155+
setTimeout(function() {
156+
assert.isTrue(el._propertiesChanged.calledTwice);
157+
el._setProperty('foo', true);
158+
setTimeout(function() {
159+
assert.isTrue(el._propertiesChanged.calledThrice);
160+
done();
161+
});
162+
});
163+
})
164+
132165
});
133166

134167
</script>

0 commit comments

Comments
 (0)