From 5c8d93c9dde1009a48a78e871b8e1fffecddeddf Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Wed, 22 Nov 2017 23:32:38 -0800 Subject: [PATCH] refactor($rootScope): consistently use noop as the default $watch listener --- src/ng/interpolate.js | 4 +--- src/ng/rootScope.js | 9 +++------ src/ngMessageFormat/messageFormatCommon.js | 4 ++-- src/ngMessageFormat/messageFormatInterpolationParts.js | 4 +--- src/ngMessageFormat/messageFormatSelector.js | 4 +--- 5 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/ng/interpolate.js b/src/ng/interpolate.js index 4a3998e77f59..30ad9e3a9ad8 100644 --- a/src/ng/interpolate.js +++ b/src/ng/interpolate.js @@ -331,9 +331,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; }); } diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index 616cc69676d0..ef51584a209c 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -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; diff --git a/src/ngMessageFormat/messageFormatCommon.js b/src/ngMessageFormat/messageFormatCommon.js index bde7d0d7730e..29ed30c4d5c8 100644 --- a/src/ngMessageFormat/messageFormatCommon.js +++ b/src/ngMessageFormat/messageFormatCommon.js @@ -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; diff --git a/src/ngMessageFormat/messageFormatInterpolationParts.js b/src/ngMessageFormat/messageFormatInterpolationParts.js index f2526f1c6ddc..422f1350753c 100644 --- a/src/ngMessageFormat/messageFormatInterpolationParts.js +++ b/src/ngMessageFormat/messageFormatInterpolationParts.js @@ -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; }; diff --git a/src/ngMessageFormat/messageFormatSelector.js b/src/ngMessageFormat/messageFormatSelector.js index 749d9fd861f4..f5b110214194 100644 --- a/src/ngMessageFormat/messageFormatSelector.js +++ b/src/ngMessageFormat/messageFormatSelector.js @@ -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; };