refactor($rootScope): consistently use noop as the default $watch listener

Closes #16343
This commit is contained in:
Jason Bedard
2017-12-05 13:26:20 -08:00
committed by Martin Staffa
parent d2511cfac0
commit 0b6ec6b3ab
5 changed files with 8 additions and 17 deletions
+1 -3
View File
@@ -333,9 +333,7 @@ function $InterpolateProvider() {
var lastValue;
return scope.$watchGroup(parseFns, /** @this */ function interpolateFnWatcher(values, oldValues) {
var currValue = compute(values);
if (isFunction(listener)) {
listener.call(this, currValue, values !== oldValues ? lastValue : currValue, scope);
}
listener.call(this, currValue, values !== oldValues ? lastValue : currValue, scope);
lastValue = currValue;
});
}
+3 -6
View File
@@ -394,14 +394,15 @@ function $RootScopeProvider() {
*/
$watch: function(watchExp, listener, objectEquality, prettyPrintExpression) {
var get = $parse(watchExp);
var fn = isFunction(listener) ? listener : noop;
if (get.$$watchDelegate) {
return get.$$watchDelegate(this, listener, objectEquality, get, watchExp);
return get.$$watchDelegate(this, fn, objectEquality, get, watchExp);
}
var scope = this,
array = scope.$$watchers,
watcher = {
fn: listener,
fn: fn,
last: initWatchVal,
get: get,
exp: prettyPrintExpression || watchExp,
@@ -410,10 +411,6 @@ function $RootScopeProvider() {
lastDirtyWatch = null;
if (!isFunction(listener)) {
watcher.fn = noop;
}
if (!array) {
array = scope.$$watchers = [];
array.$$digestWatchIndex = -1;
+2 -2
View File
@@ -34,7 +34,7 @@ function parseTextLiteral(text) {
parsedFn['$$watchDelegate'] = function watchDelegate(scope, listener, objectEquality) {
var unwatch = scope['$watch'](noop,
function textLiteralWatcher() {
if (isFunction(listener)) { listener(text, text, scope); }
listener(text, text, scope);
unwatch();
},
objectEquality);
@@ -58,7 +58,7 @@ function subtractOffset(expressionFn, offset) {
parsedFn['$$watchDelegate'] = function watchDelegate(scope, listener, objectEquality) {
unwatch = scope['$watch'](expressionFn,
function pluralExpressionWatchListener(newValue, oldValue) {
if (isFunction(listener)) { listener(minusOffset(newValue), minusOffset(oldValue), scope); }
listener(minusOffset(newValue), minusOffset(oldValue), scope);
},
objectEquality);
return unwatch;
@@ -122,9 +122,7 @@ function InterpolationPartsWatcher(interpolationParts, scope, listener, objectEq
InterpolationPartsWatcher.prototype.watchListener = function watchListener(newExpressionValues, oldExpressionValues) {
var result = this.interpolationParts.getResult(newExpressionValues);
if (isFunction(this.listener)) {
this.listener.call(null, result, newExpressionValues === oldExpressionValues ? result : this.previousResult, this.scope);
}
this.listener.call(null, result, newExpressionValues === oldExpressionValues ? result : this.previousResult, this.scope);
this.previousResult = result;
};
+1 -3
View File
@@ -66,9 +66,7 @@ MessageSelectorWatchers.prototype.expressionFnListener = function expressionFnLi
};
MessageSelectorWatchers.prototype.messageFnListener = function messageFnListener(newMessage, oldMessage) {
if (isFunction(this.listener)) {
this.listener.call(null, newMessage, newMessage === oldMessage ? newMessage : this.lastMessage, this.scope);
}
this.listener.call(null, newMessage, newMessage === oldMessage ? newMessage : this.lastMessage, this.scope);
this.lastMessage = newMessage;
};