Skip to content

Commit ce378f2

Browse files
jeffbcrosscaitp
authored andcommitted
revert chore(Scope): remove deregisterNotifier feature for $watch
This reverts commit d2f8f25. Closes angular#8542
1 parent 1b6a107 commit ce378f2

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

src/ng/interpolate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,15 @@ function $InterpolateProvider() {
301301
exp: text, //just for compatibility with regular watchers created via $watch
302302
separators: separators,
303303
expressions: expressions,
304-
$$watchDelegate: function (scope, listener, objectEquality) {
304+
$$watchDelegate: function (scope, listener, objectEquality, deregisterNotifier) {
305305
var lastValue;
306306
return scope.$watchGroup(parseFns, function interpolateFnWatcher(values, oldValues) {
307307
var currValue = compute(values);
308308
if (isFunction(listener)) {
309309
listener.call(this, currValue, values !== oldValues ? lastValue : currValue, scope);
310310
}
311311
lastValue = currValue;
312-
}, objectEquality);
312+
}, objectEquality, deregisterNotifier);
313313
}
314314
});
315315
}

src/ng/parse.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ function $ParseProvider() {
10351035
}
10361036
};
10371037

1038-
function oneTimeWatch(scope, listener, objectEquality, parsedExpression) {
1038+
function oneTimeWatch(scope, listener, objectEquality, deregisterNotifier, parsedExpression) {
10391039
var unwatch, lastValue;
10401040
return unwatch = scope.$watch(function oneTimeWatch(scope) {
10411041
return parsedExpression(scope);
@@ -1051,7 +1051,7 @@ function $ParseProvider() {
10511051
}
10521052
});
10531053
}
1054-
}, objectEquality);
1054+
}, objectEquality, deregisterNotifier);
10551055
}
10561056

10571057
function oneTimeLiteralWatch(scope, listener, objectEquality, parsedExpression) {
@@ -1078,7 +1078,7 @@ function $ParseProvider() {
10781078
}
10791079
}
10801080

1081-
function constantWatch(scope, listener, objectEquality, parsedExpression) {
1081+
function constantWatch(scope, listener, objectEquality, deregisterNotifier, parsedExpression) {
10821082
var unwatch;
10831083
return unwatch = scope.$watch(function constantWatch(scope) {
10841084
return parsedExpression(scope);
@@ -1087,7 +1087,7 @@ function $ParseProvider() {
10871087
listener.apply(this, arguments);
10881088
}
10891089
unwatch();
1090-
}, objectEquality);
1090+
}, objectEquality, deregisterNotifier);
10911091
}
10921092

10931093
function addInterceptor(parsedExpression, interceptorFn) {

src/ng/rootScope.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,15 @@ function $RootScopeProvider(){
322322
* - `scope` refers to the current scope
323323
* @param {boolean=} objectEquality Compare for object equality using {@link angular.equals} instead of
324324
* comparing for reference equality.
325+
* @param {function()=} deregisterNotifier Function to call when the deregistration function
326+
* get called.
325327
* @returns {function()} Returns a deregistration function for this listener.
326328
*/
327-
$watch: function(watchExp, listener, objectEquality) {
329+
$watch: function(watchExp, listener, objectEquality, deregisterNotifier) {
328330
var get = compileToFn(watchExp, 'watch');
329331

330332
if (get.$$watchDelegate) {
331-
return get.$$watchDelegate(this, listener, objectEquality, get);
333+
return get.$$watchDelegate(this, listener, objectEquality, deregisterNotifier, get);
332334
}
333335
var scope = this,
334336
array = scope.$$watchers,
@@ -356,6 +358,9 @@ function $RootScopeProvider(){
356358
return function deregisterWatch() {
357359
arrayRemove(array, watcher);
358360
lastDirtyWatch = null;
361+
if (isFunction(deregisterNotifier)) {
362+
deregisterNotifier();
363+
}
359364
};
360365
},
361366

test/ng/rootScopeSpec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,34 @@ describe('Scope', function() {
526526
expect(log).toEqual(['watch1', 'watchAction1', 'watch2', 'watchAction2', 'watch3', 'watchAction3',
527527
'watch2', 'watch3']);
528528
}));
529+
530+
describe('deregisterNotifier', function () {
531+
it('should call the deregisterNotifier when the watch is deregistered', inject(
532+
function($rootScope) {
533+
var notifier = jasmine.createSpy('deregisterNotifier');
534+
var listenerRemove = $rootScope.$watch('noop', noop, false, notifier);
535+
536+
expect(notifier).not.toHaveBeenCalled();
537+
538+
listenerRemove();
539+
expect(notifier).toHaveBeenCalledOnce();
540+
}));
541+
542+
543+
it('should call the deregisterNotifier when a one-time expression is stable', inject(
544+
function($rootScope) {
545+
var notifier = jasmine.createSpy('deregisterNotifier');
546+
$rootScope.$watch('::foo', noop, false, notifier);
547+
548+
expect(notifier).not.toHaveBeenCalledOnce();
549+
$rootScope.$digest();
550+
expect(notifier).not.toHaveBeenCalledOnce();
551+
552+
$rootScope.foo = 'foo';
553+
$rootScope.$digest();
554+
expect(notifier).toHaveBeenCalledOnce();
555+
}));
556+
});
529557
});
530558

531559

0 commit comments

Comments
 (0)