Skip to content

Commit 0e564ae

Browse files
author
Steve Orvell
authored
Merge pull request #5097 from Polymer/shouldPropertiesChange
[PropertiesChanged]: adds _shouldPropertiesChange
2 parents 2e39cf6 + 74907b9 commit 0e564ae

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

externs/closure-types.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ Polymer_PropertiesChanged.prototype._enableProperties = function(){};
7878
Polymer_PropertiesChanged.prototype._flushProperties = function(){};
7979
/**
8080
* @param {!Object} currentProps Bag of all current accessor values
81+
* @param {!Object} changedProps Bag of properties changed since the last
82+
call to `_propertiesChanged`
83+
* @param {!Object} oldProps Bag of previous values for each property
84+
in `changedProps`
85+
* @return {boolean}
86+
*/
87+
Polymer_PropertiesChanged.prototype._shouldPropertiesChange = function(currentProps, changedProps, oldProps){};
88+
/**
89+
* @param {!Object} currentProps Bag of all current accessor values
8190
* @param {!Object} changedProps Bag of properties changed since the last
8291
call to `_propertiesChanged`
8392
* @param {!Object} oldProps Bag of previous values for each property

lib/mixins/properties-changed.html

Lines changed: 23 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,32 @@
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.__dataOld = null;
341+
this._propertiesChanged(props, changedProps, old);
339342
}
340343
}
341344

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

test/unit/properties-changed.html

Lines changed: 32 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,20 @@
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+
});
132164
});
133165

134166
</script>

types/lib/mixins/properties-changed.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,21 @@ declare namespace Polymer {
182182
*/
183183
_flushProperties(): void;
184184

185+
/**
186+
* Called in `_flushProperties` to determine if `_propertiesChanged`
187+
* should be called. The default implementation returns true if
188+
* properties are pending. Override to customize when
189+
* `_propertiesChanged` is called.
190+
*
191+
* @param currentProps Bag of all current accessor values
192+
* @param changedProps Bag of properties changed since the last
193+
* call to `_propertiesChanged`
194+
* @param oldProps Bag of previous values for each property
195+
* in `changedProps`
196+
* @returns true if changedProps is truthy
197+
*/
198+
_shouldPropertiesChange(currentProps: object, changedProps: object, oldProps: object): boolean;
199+
185200
/**
186201
* Callback called when any properties with accessors created via
187202
* `_createPropertyAccessor` have been set.

0 commit comments

Comments
 (0)