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

Revert removal of deregisterNotifier #8542

Closed
wants to merge 3 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
4 changes: 2 additions & 2 deletions src/ng/interpolate.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,15 @@ function $InterpolateProvider() {
exp: text, //just for compatibility with regular watchers created via $watch
separators: separators,
expressions: expressions,
$$watchDelegate: function (scope, listener, objectEquality) {
$$watchDelegate: function (scope, listener, objectEquality, deregisterNotifier) {
var lastValue;
return scope.$watchGroup(parseFns, function interpolateFnWatcher(values, oldValues) {
var currValue = compute(values);
if (isFunction(listener)) {
listener.call(this, currValue, values !== oldValues ? lastValue : currValue, scope);
}
lastValue = currValue;
}, objectEquality);
}, objectEquality, deregisterNotifier);
}
});
}
Expand Down
10 changes: 5 additions & 5 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ function $ParseProvider() {
}
};

function oneTimeWatch(scope, listener, objectEquality, parsedExpression) {
function oneTimeWatch(scope, listener, objectEquality, deregisterNotifier, parsedExpression) {
var unwatch, lastValue;
return unwatch = scope.$watch(function oneTimeWatch(scope) {
return parsedExpression(scope);
Expand All @@ -1051,10 +1051,10 @@ function $ParseProvider() {
}
});
}
}, objectEquality);
}, objectEquality, deregisterNotifier);
}

function oneTimeLiteralWatch(scope, listener, objectEquality, parsedExpression) {
function oneTimeLiteralWatch(scope, listener, objectEquality, deregisterNotifier, parsedExpression) {
var unwatch;
return unwatch = scope.$watch(function oneTimeWatch(scope) {
return parsedExpression(scope);
Expand All @@ -1078,7 +1078,7 @@ function $ParseProvider() {
}
}

function constantWatch(scope, listener, objectEquality, parsedExpression) {
function constantWatch(scope, listener, objectEquality, deregisterNotifier, parsedExpression) {
var unwatch;
return unwatch = scope.$watch(function constantWatch(scope) {
return parsedExpression(scope);
Expand All @@ -1087,7 +1087,7 @@ function $ParseProvider() {
listener.apply(this, arguments);
}
unwatch();
}, objectEquality);
}, objectEquality, deregisterNotifier);
}

function addInterceptor(parsedExpression, interceptorFn) {
Expand Down
43 changes: 23 additions & 20 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,15 @@ function $RootScopeProvider(){
* - `scope` refers to the current scope
* @param {boolean=} objectEquality Compare for object equality using {@link angular.equals} instead of
* comparing for reference equality.
* @param {function()=} deregisterNotifier Function to call when the deregistration function
* get called.
* @returns {function()} Returns a deregistration function for this listener.
*/
$watch: function(watchExp, listener, objectEquality) {
$watch: function(watchExp, listener, objectEquality, deregisterNotifier) {
var get = compileToFn(watchExp, 'watch');

if (get.$$watchDelegate) {
return get.$$watchDelegate(this, listener, objectEquality, get);
return get.$$watchDelegate(this, listener, objectEquality, deregisterNotifier, get);
}
var scope = this,
array = scope.$$watchers,
Expand Down Expand Up @@ -356,6 +358,9 @@ function $RootScopeProvider(){
return function deregisterWatch() {
arrayRemove(array, watcher);
lastDirtyWatch = null;
if (isFunction(deregisterNotifier)) {
deregisterNotifier();
}
};
},

Expand Down Expand Up @@ -388,9 +393,9 @@ function $RootScopeProvider(){
var oldValues = new Array(watchExpressions.length);
var newValues = new Array(watchExpressions.length);
var deregisterFns = [];
var changeCount = 0;
var self = this;
var changeReactionScheduled = false;
var firstRun = true;
var masterUnwatch;

if (watchExpressions.length === 1) {
// Special case size of one
Expand All @@ -402,31 +407,29 @@ function $RootScopeProvider(){
}

forEach(watchExpressions, function (expr, i) {
var unwatchFn = self.$watch(expr, function watchGroupSubAction(value, oldValue) {
var unwatch = self.$watch(expr, function watchGroupSubAction(value, oldValue) {
newValues[i] = value;
oldValues[i] = oldValue;
if (!changeReactionScheduled) {
changeReactionScheduled = true;
self.$evalAsync(watchGroupAction);
changeCount++;
}, false, function watchGroupDeregNotifier() {
arrayRemove(deregisterFns, unwatch);
if (!deregisterFns.length) {
masterUnwatch();
}
});
deregisterFns.push(unwatchFn);
});

function watchGroupAction() {
changeReactionScheduled = false;
deregisterFns.push(unwatch);
}, this);

if (firstRun) {
firstRun = false;
listener(newValues, newValues, self);
} else {
listener(newValues, oldValues, self);
}
}
masterUnwatch = self.$watch(function watchGroupChangeWatch() {
return changeCount;
}, function watchGroupChangeAction(value, oldValue) {
listener(newValues, (value === oldValue) ? newValues : oldValues, self);
});

return function deregisterWatchGroup() {
while (deregisterFns.length) {
deregisterFns.shift()();
deregisterFns[0]();
}
};
},
Expand Down
28 changes: 14 additions & 14 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2893,21 +2893,21 @@ describe('$compile', function() {

inject(function($rootScope) {
compile('<div other-tpl-dir param1="::foo" param2="bar"></div>');
expect(countWatches($rootScope)).toEqual(6); // 4 -> template watch group, 2 -> '='
expect(countWatches($rootScope)).toEqual(7); // 5 -> template watch group, 2 -> '='
$rootScope.$digest();
expect(element.html()).toBe('1:;2:;3:;4:');
expect(countWatches($rootScope)).toEqual(6);
expect(countWatches($rootScope)).toEqual(7);

$rootScope.foo = 'foo';
$rootScope.$digest();
expect(element.html()).toBe('1:foo;2:;3:foo;4:');
expect(countWatches($rootScope)).toEqual(4);
expect(countWatches($rootScope)).toEqual(5);

$rootScope.foo = 'baz';
$rootScope.bar = 'bar';
$rootScope.$digest();
expect(element.html()).toBe('1:foo;2:bar;3:foo;4:bar');
expect(countWatches($rootScope)).toEqual(3);
expect(countWatches($rootScope)).toEqual(4);

$rootScope.bar = 'baz';
$rootScope.$digest();
Expand All @@ -2927,21 +2927,21 @@ describe('$compile', function() {

inject(function($rootScope) {
compile('<div other-tpl-dir param1="{{::foo}}" param2="{{bar}}"></div>');
expect(countWatches($rootScope)).toEqual(6); // 4 -> template watch group, 2 -> {{ }}
expect(countWatches($rootScope)).toEqual(7); // 5 -> template watch group, 2 -> {{ }}
$rootScope.$digest();
expect(element.html()).toBe('1:;2:;3:;4:');
expect(countWatches($rootScope)).toEqual(4); // (- 2) -> bind-once in template
expect(countWatches($rootScope)).toEqual(5); // (- 2) -> bind-once in template

$rootScope.foo = 'foo';
$rootScope.$digest();
expect(element.html()).toBe('1:foo;2:;3:;4:');
expect(countWatches($rootScope)).toEqual(3);
expect(countWatches($rootScope)).toEqual(4);

$rootScope.foo = 'baz';
$rootScope.bar = 'bar';
$rootScope.$digest();
expect(element.html()).toBe('1:foo;2:bar;3:;4:');
expect(countWatches($rootScope)).toEqual(3);
expect(countWatches($rootScope)).toEqual(4);

$rootScope.bar = 'baz';
$rootScope.$digest();
Expand All @@ -2964,18 +2964,18 @@ describe('$compile', function() {
compile('<div other-tpl-dir param1="::foo" param2="bar"></div>');
$rootScope.$digest();
expect(element.html()).toBe('1:;2:;3:;4:');
expect(countWatches($rootScope)).toEqual(6); // 4 -> template watch group, 2 -> '='
expect(countWatches($rootScope)).toEqual(7); // 5 -> template watch group, 2 -> '='

$rootScope.foo = 'foo';
$rootScope.$digest();
expect(element.html()).toBe('1:foo;2:;3:foo;4:');
expect(countWatches($rootScope)).toEqual(4);
expect(countWatches($rootScope)).toEqual(5);

$rootScope.foo = 'baz';
$rootScope.bar = 'bar';
$rootScope.$digest();
expect(element.html()).toBe('1:foo;2:bar;3:foo;4:bar');
expect(countWatches($rootScope)).toEqual(3);
expect(countWatches($rootScope)).toEqual(4);

$rootScope.bar = 'baz';
$rootScope.$digest();
Expand All @@ -2998,18 +2998,18 @@ describe('$compile', function() {
compile('<div other-tpl-dir param1="{{::foo}}" param2="{{bar}}"></div>');
$rootScope.$digest();
expect(element.html()).toBe('1:;2:;3:;4:');
expect(countWatches($rootScope)).toEqual(4); // (4 - 2) -> template watch group, 2 -> {{ }}
expect(countWatches($rootScope)).toEqual(5); // (5 - 2) -> template watch group, 2 -> {{ }}

$rootScope.foo = 'foo';
$rootScope.$digest();
expect(element.html()).toBe('1:foo;2:;3:;4:');
expect(countWatches($rootScope)).toEqual(3);
expect(countWatches($rootScope)).toEqual(4);

$rootScope.foo = 'baz';
$rootScope.bar = 'bar';
$rootScope.$digest();
expect(element.html()).toBe('1:foo;2:bar;3:;4:');
expect(countWatches($rootScope)).toEqual(3);
expect(countWatches($rootScope)).toEqual(4);

$rootScope.bar = 'baz';
$rootScope.$digest();
Expand Down
4 changes: 2 additions & 2 deletions test/ng/directive/ngBindSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ describe('ngBind*', function() {
it('should one-time bind the expressions that start with ::', inject(function($rootScope, $compile) {
element = $compile('<div ng-bind-template="{{::hello}} {{::name}}!"></div>')($rootScope);
$rootScope.name = 'Misko';
expect($rootScope.$$watchers.length).toEqual(2);
expect($rootScope.$$watchers.length).toEqual(3);
$rootScope.$digest();
expect(element.hasClass('ng-binding')).toEqual(true);
expect(element.text()).toEqual(' Misko!');
expect($rootScope.$$watchers.length).toEqual(1);
expect($rootScope.$$watchers.length).toEqual(2);
$rootScope.hello = 'Hello';
$rootScope.name = 'Lucas';
$rootScope.$digest();
Expand Down
34 changes: 31 additions & 3 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ describe('Scope', function() {

it('should clean up stable watches from $watchGroup', inject(function($rootScope) {
$rootScope.$watchGroup(['::foo', '::bar'], function() {});
expect($rootScope.$$watchers.length).toEqual(2);
expect($rootScope.$$watchers.length).toEqual(3);

$rootScope.$digest();
expect($rootScope.$$watchers.length).toEqual(2);
expect($rootScope.$$watchers.length).toEqual(3);

$rootScope.foo = 'foo';
$rootScope.$digest();
expect($rootScope.$$watchers.length).toEqual(1);
expect($rootScope.$$watchers.length).toEqual(2);

$rootScope.bar = 'bar';
$rootScope.$digest();
Expand Down Expand Up @@ -526,6 +526,34 @@ describe('Scope', function() {
expect(log).toEqual(['watch1', 'watchAction1', 'watch2', 'watchAction2', 'watch3', 'watchAction3',
'watch2', 'watch3']);
}));

describe('deregisterNotifier', function () {
it('should call the deregisterNotifier when the watch is deregistered', inject(
function($rootScope) {
var notifier = jasmine.createSpy('deregisterNotifier');
var listenerRemove = $rootScope.$watch('noop', noop, false, notifier);

expect(notifier).not.toHaveBeenCalled();

listenerRemove();
expect(notifier).toHaveBeenCalledOnce();
}));


it('should call the deregisterNotifier when a one-time expression is stable', inject(
function($rootScope) {
var notifier = jasmine.createSpy('deregisterNotifier');
$rootScope.$watch('::foo', noop, false, notifier);

expect(notifier).not.toHaveBeenCalledOnce();
$rootScope.$digest();
expect(notifier).not.toHaveBeenCalledOnce();

$rootScope.foo = 'foo';
$rootScope.$digest();
expect(notifier).toHaveBeenCalledOnce();
}));
});
});


Expand Down