Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($parse): fix infinite digest errors when watching objects with .valueOf in literals #15867

Merged
merged 1 commit into from
Mar 31, 2017
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
4 changes: 2 additions & 2 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1788,14 +1788,14 @@ function $ParseProvider() {
return newValue === oldValueOfValue;
}

if (typeof newValue === 'object' && !compareObjectIdentity) {
if (typeof newValue === 'object') {

// attempt to convert the value to a primitive type
// TODO(docs): add a note to docs that by implementing valueOf even objects and arrays can
// be cheaply dirty-checked
newValue = getValueOf(newValue);

if (typeof newValue === 'object') {
if (typeof newValue === 'object' && !compareObjectIdentity) {
// objects/arrays are not supported - deep-watching them would be too expensive
return false;
}
Expand Down
91 changes: 91 additions & 0 deletions test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2872,6 +2872,40 @@ describe('parser', function() {
expect(called).toBe(true);
}));

it('should not invoke interceptorFns unless the input.valueOf changes even if the instance changes', inject(function($parse) {
var called = false;
function interceptor(v) {
called = true;
return v;
}
scope.$watch($parse('a', interceptor));
scope.a = new Date();
scope.$digest();
expect(called).toBe(true);

called = false;
scope.a = new Date(scope.a.valueOf());
scope.$digest();
expect(called).toBe(false);
}));

it('should invoke interceptorFns if input.valueOf changes even if the instance does not', inject(function($parse) {
var called = false;
function interceptor(v) {
called = true;
return v;
}
scope.$watch($parse('a', interceptor));
scope.a = new Date();
scope.$digest();
expect(called).toBe(true);

called = false;
scope.a.setTime(scope.a.getTime() + 1);
scope.$digest();
expect(called).toBe(true);
}));

it('should invoke interceptors when the expression is `undefined`', inject(function($parse) {
var called = false;
function interceptor(v) {
Expand Down Expand Up @@ -3040,6 +3074,63 @@ describe('parser', function() {
expect(called).toBe(true);
}));

it('should not reevaluate literals with non-primitive input that does support valueOf()',
inject(function($parse) {

var date = scope.date = new Date();

var parsed = $parse('[date]');
var watcherCalls = 0;
scope.$watch(parsed, function(input) {
expect(input[0]).toBe(date);
watcherCalls++;
});

scope.$digest();
expect(watcherCalls).toBe(1);

scope.$digest();
expect(watcherCalls).toBe(1);
}));

it('should not reevaluate literals with non-primitive input that does support valueOf()' +
' when the instance changes but valueOf() does not', inject(function($parse) {

scope.date = new Date(1234567890123);

var parsed = $parse('[date]');
var watcherCalls = 0;
scope.$watch(parsed, function(input) {
watcherCalls++;
});

scope.$digest();
expect(watcherCalls).toBe(1);

scope.date = new Date(1234567890123);
scope.$digest();
expect(watcherCalls).toBe(1);
}));

it('should reevaluate literals with non-primitive input that does support valueOf()' +
' when the instance does not change but valueOf() does', inject(function($parse) {

scope.date = new Date(1234567890123);

var parsed = $parse('[date]');
var watcherCalls = 0;
scope.$watch(parsed, function(input) {
watcherCalls++;
});

scope.$digest();
expect(watcherCalls).toBe(1);

scope.date.setTime(scope.date.getTime() + 1);
scope.$digest();
expect(watcherCalls).toBe(2);
}));

it('should continue with the evaluation of the expression without invoking computed parts',
inject(function($parse) {
var value = 'foo';
Expand Down