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

Commit

Permalink
perf($rootScope): allow $watchCollection use of expression input watc…
Browse files Browse the repository at this point in the history
…hing

By adding a $$pure flag to the $watchCollectionInterceptor to shallow
watch all inputs regardless of type when watching an object/array
literal.
  • Loading branch information
jbedard committed Sep 18, 2017
1 parent 5728076 commit 97b00ca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,7 @@ function $ParseProvider() {
return second(first(value));
}
chainedInterceptor.$stateful = first.$stateful || second.$stateful;
chainedInterceptor.$$pure = first.$$pure && second.$$pure;

return chainedInterceptor;
}
Expand Down Expand Up @@ -1979,14 +1980,18 @@ function $ParseProvider() {
// If the expression itself has no inputs then use the full expression as an input.
if (!interceptorFn.$stateful) {
useInputs = !parsedExpression.inputs;
fn.inputs = (parsedExpression.inputs ? parsedExpression.inputs : [parsedExpression]).map(function(e) {
fn.inputs = parsedExpression.inputs ? parsedExpression.inputs : [parsedExpression];

if (!interceptorFn.$$pure) {
fn.inputs = fn.inputs.map(function(e) {
// Remove the isPure flag of inputs when it is not absolute because they are now wrapped in a
// potentially non-pure interceptor function.
// non-pure interceptor function.
if (e.isPure === PURITY_RELATIVE) {
return function depurifier(s) { return e(s); };
}
return e;
});
}
}

return addWatchDelegate(fn);
Expand Down
6 changes: 5 additions & 1 deletion src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,11 @@ function $RootScopeProvider() {
* de-registration function is executed, the internal watch operation is terminated.
*/
$watchCollection: function(obj, listener) {
$watchCollectionInterceptor.$stateful = true;
// Mark the interceptor as
// ... $$pure when literal since the instance will change when any input changes
$watchCollectionInterceptor.$$pure = $parse(obj).literal;
// ... $stateful when non-literal since we must read the state of the collection
$watchCollectionInterceptor.$stateful = !$watchCollectionInterceptor.$$pure;

var self = this;
// the current value, updated on each dirty-check run
Expand Down

0 comments on commit 97b00ca

Please sign in to comment.