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

Commit 394b185

Browse files
jbedardNarretz
authored andcommitted
refactor($rootScope): consistently use noop as the default $watch listener
Closes #16343
1 parent da72477 commit 394b185

File tree

5 files changed

+8
-17
lines changed

5 files changed

+8
-17
lines changed

src/ng/interpolate.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,7 @@ function $InterpolateProvider() {
331331
var lastValue;
332332
return scope.$watchGroup(parseFns, /** @this */ function interpolateFnWatcher(values, oldValues) {
333333
var currValue = compute(values);
334-
if (isFunction(listener)) {
335-
listener.call(this, currValue, values !== oldValues ? lastValue : currValue, scope);
336-
}
334+
listener.call(this, currValue, values !== oldValues ? lastValue : currValue, scope);
337335
lastValue = currValue;
338336
});
339337
}

src/ng/rootScope.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,15 @@ function $RootScopeProvider() {
394394
*/
395395
$watch: function(watchExp, listener, objectEquality, prettyPrintExpression) {
396396
var get = $parse(watchExp);
397+
var fn = isFunction(listener) ? listener : noop;
397398

398399
if (get.$$watchDelegate) {
399-
return get.$$watchDelegate(this, listener, objectEquality, get, watchExp);
400+
return get.$$watchDelegate(this, fn, objectEquality, get, watchExp);
400401
}
401402
var scope = this,
402403
array = scope.$$watchers,
403404
watcher = {
404-
fn: listener,
405+
fn: fn,
405406
last: initWatchVal,
406407
get: get,
407408
exp: prettyPrintExpression || watchExp,
@@ -410,10 +411,6 @@ function $RootScopeProvider() {
410411

411412
lastDirtyWatch = null;
412413

413-
if (!isFunction(listener)) {
414-
watcher.fn = noop;
415-
}
416-
417414
if (!array) {
418415
array = scope.$$watchers = [];
419416
array.$$digestWatchIndex = -1;

src/ngMessageFormat/messageFormatCommon.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function parseTextLiteral(text) {
3434
parsedFn['$$watchDelegate'] = function watchDelegate(scope, listener, objectEquality) {
3535
var unwatch = scope['$watch'](noop,
3636
function textLiteralWatcher() {
37-
if (isFunction(listener)) { listener(text, text, scope); }
37+
listener(text, text, scope);
3838
unwatch();
3939
},
4040
objectEquality);
@@ -58,7 +58,7 @@ function subtractOffset(expressionFn, offset) {
5858
parsedFn['$$watchDelegate'] = function watchDelegate(scope, listener, objectEquality) {
5959
unwatch = scope['$watch'](expressionFn,
6060
function pluralExpressionWatchListener(newValue, oldValue) {
61-
if (isFunction(listener)) { listener(minusOffset(newValue), minusOffset(oldValue), scope); }
61+
listener(minusOffset(newValue), minusOffset(oldValue), scope);
6262
},
6363
objectEquality);
6464
return unwatch;

src/ngMessageFormat/messageFormatInterpolationParts.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ function InterpolationPartsWatcher(interpolationParts, scope, listener, objectEq
122122

123123
InterpolationPartsWatcher.prototype.watchListener = function watchListener(newExpressionValues, oldExpressionValues) {
124124
var result = this.interpolationParts.getResult(newExpressionValues);
125-
if (isFunction(this.listener)) {
126-
this.listener.call(null, result, newExpressionValues === oldExpressionValues ? result : this.previousResult, this.scope);
127-
}
125+
this.listener.call(null, result, newExpressionValues === oldExpressionValues ? result : this.previousResult, this.scope);
128126
this.previousResult = result;
129127
};
130128

src/ngMessageFormat/messageFormatSelector.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ MessageSelectorWatchers.prototype.expressionFnListener = function expressionFnLi
6666
};
6767

6868
MessageSelectorWatchers.prototype.messageFnListener = function messageFnListener(newMessage, oldMessage) {
69-
if (isFunction(this.listener)) {
70-
this.listener.call(null, newMessage, newMessage === oldMessage ? newMessage : this.lastMessage, this.scope);
71-
}
69+
this.listener.call(null, newMessage, newMessage === oldMessage ? newMessage : this.lastMessage, this.scope);
7270
this.lastMessage = newMessage;
7371
};
7472

0 commit comments

Comments
 (0)