Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle NaN correctly in bindings #1811

Merged
merged 1 commit into from
Jun 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/lib/bind/accessors.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@

_propertySet: function(property, value, effects) {
var old = this.__data__[property];
if (old !== value) {
// NaN is always not equal to itself,
// if old and value are both NaN we treat them as equal
// x === x is 10x faster, and equivalent to !isNaN(x)
if (old !== value && (old === old || value === value)) {
this.__data__[property] = value;
if (typeof value == 'object') {
this._clearPath(property);
Expand Down
7 changes: 5 additions & 2 deletions src/standard/notify-path.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@
notifyPath: function(path, value, fromAbove) {
var old = this._propertySet(path, value);
// manual dirty checking for now...
if (old !== value) {
// NaN is always not equal to itself,
// if old and value are both NaN we treat them as equal
// x === x is 10x faster, and equivalent to !isNaN(x)
if (old !== value && (old === old || value === value)) {
// console.group((this.localName || this.dataHost.id + '-' + this.dataHost.dataHost.index) + '#' + (this.id || this.index) + ' ' + path, value);
// Take path effects at this level for exact path matches,
// and notify down for any bindings to a subset of this path
Expand Down Expand Up @@ -463,4 +466,4 @@
})();


</script>
</script>
12 changes: 11 additions & 1 deletion test/unit/bind-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
notify: true,
observer: 'readonlyvalueChanged'
},
notifierWithoutComputing: {
type: Number,
observer: 'notifierWithoutComputingChanged',
notify: true,
value: 0
},
add: {
value: 20
},
Expand Down Expand Up @@ -112,7 +118,8 @@
computedFromMultipleValuesChanged: 0,
multipleDepChangeHandler: 0,
customEventValueChanged: 0,
customEventObjectValueChanged: 0
customEventObjectValueChanged: 0,
notifierWithoutComputingChanged: 0
};
},
clearObserverCounts: function() {
Expand Down Expand Up @@ -158,6 +165,9 @@
assert.equal(old, this._readonlyvalue, 'observer old argument wrong');
this._readonlyvalue = val;
},
notifierWithoutComputingChanged: function() {
this.observerCounts.notifierWithoutComputingChanged++;
},
computeNotifyingValue: function(val) {
return val + 2;
},
Expand Down
13 changes: 13 additions & 0 deletions test/unit/bind.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@
assert.equal(el.observerCounts.computedvalueChanged, 1, 'observer not called');
});

test('NaN does not loop observers', function() {
el.clearObserverCounts();
el.addEventListener('notifierwithoutcomputing-changed', function(e) {
if (el.observerCounts.notifierWithoutComputingChanged >= 3) {
throw new Error('infinite loop!');
}
});
el.notifierWithoutComputing = NaN;
assert.equal(el.observerCounts.notifierWithoutComputingChanged, 1,
'NaN was not handled as expected');
});


test('computed notification sent', function() {
var notified = 0;
el.addEventListener('computednotifyingvalue-changed', function(e) {
Expand Down