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

watchCollection: Return correct oldCollection argument to listener #5661

Closed
wants to merge 6 commits into from
Closed
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
111 changes: 65 additions & 46 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,79 +416,98 @@ function $RootScopeProvider(){
var self = this;
var oldValue;
var newValue;
var changeDetected = 0;
var changeFlipFlop = 0;
var objGetter = $parse(obj);
var internalArray = [];
var internalObject = {};
var oldLength = 0;
var internalLength = 0;

// Holds simple value or reference to internalArray or internalObject.
// The special initial value is used to ensure that the listener is called
// when the watch is established and that oldValue = newValue.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= -> ===

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Igor,
I have simplified the old value checking. Let me know what you think.

On 16 March 2014 02:21, Igor Minar [email protected] wrote:

In src/ng/rootScope.js:

     var objGetter = $parse(obj);
     var internalArray = [];
     var internalObject = {};
  •    var oldLength = 0;
    
  •    var internalLength = 0;
    
  •    // Holds simple value or reference to internalArray or internalObject.
    
  •    // The special initial value is used to ensure that the listener is called
    
  •    // when the watch is established and that oldValue = newValue.
    

= -> ===

Reply to this email directly or view it on GitHubhttps://github.com//pull/5661/files#r10635231
.

var internalValue = initWatchVal;

function $watchCollectionWatch() {
var newLength, key, i, changeDetected;

newValue = objGetter(self);
var newLength, key;
oldValue = internalValue;
changeDetected = 0;

if (!isObject(newValue)) {
if (oldValue !== newValue) {
oldValue = newValue;
if (internalValue !== newValue) {
internalValue = newValue;
changeDetected++;
}
} else if (isArrayLike(newValue)) {
if (oldValue !== internalArray) {
// we are transitioning from something which was not an array into array.
oldValue = internalArray;
oldLength = oldValue.length = 0;
changeDetected++;
}

newLength = newValue.length;

if (oldLength !== newLength) {
// if lengths do not match we need to trigger change notification
if (internalValue !== internalArray) {
// we are transitioning from something which was not an array into array.
changeDetected++;
oldValue.length = oldLength = newLength;
}
// copy the items to oldValue and look for changes.
for (var i = 0; i < newLength; i++) {
if (oldValue[i] !== newValue[i]) {
} else {
if (internalLength !== newLength) {
// if lengths do not match we need to trigger change notification
changeDetected++;
oldValue[i] = newValue[i];
} else {
// look for item changes
for (i = 0; i < newLength; i++) {
if (internalValue[i] !== newValue[i]) {
changeDetected++;
break;
}
}
}
}
if (changeDetected) {
// copy the items to array cache
internalValue = internalArray = [];
internalValue.length = internalLength = newLength;
for (i = 0; i < newLength; i++) {
internalValue[i] = newValue[i];
}
}
} else {
if (oldValue !== internalObject) {
// we are transitioning from something which was not an object into object.
oldValue = internalObject = {};
oldLength = 0;
if (internalValue !== internalObject) {
// we are transitioning from something which was not an object into object
changeDetected++;
}
// copy the items to oldValue and look for changes.
newLength = 0;
for (key in newValue) {
if (newValue.hasOwnProperty(key)) {
newLength++;
if (oldValue.hasOwnProperty(key)) {
if (oldValue[key] !== newValue[key]) {
} else {
// look for item changes
newLength = 0;
for (key in newValue) {
if (newValue.hasOwnProperty(key)) {
newLength++;
if (! (internalValue.hasOwnProperty(key) &&
internalValue[key] === newValue[key])) {
changeDetected++;
oldValue[key] = newValue[key];
break;
}
} else {
oldLength++;
oldValue[key] = newValue[key];
changeDetected++;
}
}
if (internalLength !== newLength) {
changeDetected++;
}
}
if (oldLength > newLength) {
// we used to have more keys, need to find them and destroy them.
changeDetected++;
for(key in oldValue) {
if (oldValue.hasOwnProperty(key) && !newValue.hasOwnProperty(key)) {
oldLength--;
delete oldValue[key];
if (changeDetected) {
// copy the items to object cache
internalValue = internalObject = {};
internalLength = 0;
for (key in newValue) {
if (newValue.hasOwnProperty(key)) {
internalLength++;
internalValue[key] = newValue[key];
}
}
}
}
return changeDetected;

if (changeDetected) {
changeFlipFlop = 1 - changeFlipFlop;
if (oldValue === initWatchVal) {
oldValue = newValue;
}
}

return changeFlipFlop;
}

function $watchCollectionAction() {
Expand Down
53 changes: 40 additions & 13 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,29 +454,44 @@ describe('Scope', function() {
}));
});


describe('$watchCollection', function() {
var log, $rootScope, deregister;
var log, $rootScope, deregister, prevval, firstWatch, oldValueCorrect;

beforeEach(inject(function (_$rootScope_) {
log = [];
$rootScope = _$rootScope_;
firstWatch = true;
oldValueCorrect = true;
deregister = $rootScope.$watchCollection('obj', function listener(obj, oldobj) {
log.push(toJson(obj));
if (firstWatch) {
firstWatch = false;
if (toJson(obj) !== toJson(oldobj)) {
oldValueCorrect = false;
}
} else {
if (toJson(oldobj) !== prevval) {
oldValueCorrect = false;
}
}
prevval = toJson(obj);
});
}));

beforeEach(inject(function(_$rootScope_) {
log = [];
$rootScope = _$rootScope_;
deregister = $rootScope.$watchCollection('obj', function logger(obj) {
log.push(toJson(obj));
afterEach(function () {
deregister();
});
}));


it('should not trigger if nothing change', inject(function($rootScope) {
it('should not trigger if nothing change', function() {
$rootScope.$digest();
expect(log).toEqual([undefined]);

$rootScope.$digest();
expect(log).toEqual([undefined]);
}));
});


it('should allow deregistration', inject(function($rootScope) {
it('should allow deregistration', function() {
$rootScope.obj = [];
$rootScope.$digest();

Expand All @@ -487,7 +502,7 @@ describe('Scope', function() {

$rootScope.$digest();
expect(log).toEqual(['[]']);
}));
});


describe('array', function() {
Expand All @@ -511,6 +526,9 @@ describe('Scope', function() {
$rootScope.obj = undefined;
$rootScope.$digest();
expect(log).toEqual(['"test"', '[]', '{}', '[]', undefined]);

expect(oldValueCorrect).toBe(true);

});


Expand Down Expand Up @@ -554,6 +572,9 @@ describe('Scope', function() {
log = [];
$rootScope.$digest();
expect(log).toEqual([ '[{},[]]' ]);

expect(oldValueCorrect).toBe(true);

});

it('should watch array-like objects like arrays', function () {
Expand All @@ -571,6 +592,9 @@ describe('Scope', function() {
$rootScope.arrayLikeObject = document.getElementsByTagName('a');
$rootScope.$digest();
expect(arrayLikelog).toEqual(['x', 'y']);

expect(oldValueCorrect).toBe(true);

});
});

Expand Down Expand Up @@ -627,6 +651,9 @@ describe('Scope', function() {
log = [];
$rootScope.$digest();
expect(log).toEqual([ '{"b":[],"c":"B"}' ]);

expect(oldValueCorrect).toBe(true);

});
});
});
Expand Down