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

Commit 189461f

Browse files
jbedardNarretz
authored andcommitted
fix($parse): standardize one-time literal vs non-literal and interceptors
Previously literal one-time bindings did not use the expression `inputs`, causing infinite digest issues with literal values. This often forces the use of deepEquals when watching one-time literals. `ng-class` is one example of deepEquals which is no longer required. This one-time/literal behavior is now also consistently propogated through interceptors. Closes #15858
1 parent 93879b3 commit 189461f

File tree

3 files changed

+35
-80
lines changed

3 files changed

+35
-80
lines changed

src/ng/directive/ngClass.js

+8-45
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ function classDirective(name, selector) {
1414
return {
1515
restrict: 'AC',
1616
link: function(scope, element, attr) {
17-
var expression = attr[name].trim();
18-
var isOneTime = (expression.charAt(0) === ':') && (expression.charAt(1) === ':');
19-
20-
var watchInterceptor = isOneTime ? toFlatValue : toClassString;
21-
var watchExpression = $parse(expression, watchInterceptor);
22-
var watchAction = isOneTime ? ngClassOneTimeWatchAction : ngClassWatchAction;
23-
2417
var classCounts = element.data('$classCounts');
2518
var oldModulo = true;
2619
var oldClassString;
@@ -43,7 +36,7 @@ function classDirective(name, selector) {
4336
scope.$watch(indexWatchExpression, ngClassIndexWatchAction);
4437
}
4538

46-
scope.$watch(watchExpression, watchAction, isOneTime);
39+
scope.$watch($parse(attr[name], toClassString), ngClassWatchAction);
4740

4841
function addClasses(classString) {
4942
classString = digestClassCounts(split(classString), 1);
@@ -85,9 +78,9 @@ function classDirective(name, selector) {
8578
}
8679

8780
function ngClassIndexWatchAction(newModulo) {
88-
// This watch-action should run before the `ngClass[OneTime]WatchAction()`, thus it
81+
// This watch-action should run before the `ngClassWatchAction()`, thus it
8982
// adds/removes `oldClassString`. If the `ngClass` expression has changed as well, the
90-
// `ngClass[OneTime]WatchAction()` will update the classes.
83+
// `ngClassWatchAction()` will update the classes.
9184
if (newModulo === selector) {
9285
addClasses(oldClassString);
9386
} else {
@@ -97,15 +90,13 @@ function classDirective(name, selector) {
9790
oldModulo = newModulo;
9891
}
9992

100-
function ngClassOneTimeWatchAction(newClassValue) {
101-
var newClassString = toClassString(newClassValue);
102-
103-
if (newClassString !== oldClassString) {
104-
ngClassWatchAction(newClassString);
105-
}
93+
function ngClassWatchAction(newClassString) {
94+
// When using a one-time binding the newClassString will return
95+
// the pre-interceptor value until the one-time is complete
96+
if (!isString(newClassString)) {
97+
newClassString = toClassString(newClassString);
10698
}
10799

108-
function ngClassWatchAction(newClassString) {
109100
if (oldModulo === selector) {
110101
updateClasses(oldClassString, newClassString);
111102
}
@@ -152,34 +143,6 @@ function classDirective(name, selector) {
152143

153144
return classString;
154145
}
155-
156-
function toFlatValue(classValue) {
157-
var flatValue = classValue;
158-
159-
if (isArray(classValue)) {
160-
flatValue = classValue.map(toFlatValue);
161-
} else if (isObject(classValue)) {
162-
var hasUndefined = false;
163-
164-
flatValue = Object.keys(classValue).filter(function(key) {
165-
var value = classValue[key];
166-
167-
if (!hasUndefined && isUndefined(value)) {
168-
hasUndefined = true;
169-
}
170-
171-
return value;
172-
});
173-
174-
if (hasUndefined) {
175-
// Prevent the `oneTimeLiteralWatchInterceptor` from unregistering
176-
// the watcher, by including at least one `undefined` value.
177-
flatValue.push(undefined);
178-
}
179-
}
180-
181-
return flatValue;
182-
}
183146
}
184147

185148
/**

src/ng/parse.js

+20-32
Original file line numberDiff line numberDiff line change
@@ -1765,8 +1765,8 @@ function $ParseProvider() {
17651765
if (parsedExpression.constant) {
17661766
parsedExpression.$$watchDelegate = constantWatchDelegate;
17671767
} else if (oneTime) {
1768-
parsedExpression.$$watchDelegate = parsedExpression.literal ?
1769-
oneTimeLiteralWatchDelegate : oneTimeWatchDelegate;
1768+
parsedExpression.oneTime = true;
1769+
parsedExpression.$$watchDelegate = oneTimeWatchDelegate;
17701770
} else if (parsedExpression.inputs) {
17711771
parsedExpression.$$watchDelegate = inputsWatchDelegate;
17721772
}
@@ -1852,6 +1852,7 @@ function $ParseProvider() {
18521852
}
18531853

18541854
function oneTimeWatchDelegate(scope, listener, objectEquality, parsedExpression, prettyPrintExpression) {
1855+
var isDone = parsedExpression.literal ? isAllDefined : isDefined;
18551856
var unwatch, lastValue;
18561857
if (parsedExpression.inputs) {
18571858
unwatch = inputsWatchDelegate(scope, oneTimeListener, objectEquality, parsedExpression, prettyPrintExpression);
@@ -1868,42 +1869,23 @@ function $ParseProvider() {
18681869
if (isFunction(listener)) {
18691870
listener(value, old, scope);
18701871
}
1871-
if (isDefined(value)) {
1872+
if (isDone(value)) {
18721873
scope.$$postDigest(function() {
1873-
if (isDefined(lastValue)) {
1874+
if (isDone(lastValue)) {
18741875
unwatch();
18751876
}
18761877
});
18771878
}
18781879
}
18791880
}
18801881

1881-
function oneTimeLiteralWatchDelegate(scope, listener, objectEquality, parsedExpression) {
1882-
var unwatch, lastValue;
1883-
unwatch = scope.$watch(function oneTimeWatch(scope) {
1884-
return parsedExpression(scope);
1885-
}, function oneTimeListener(value, old, scope) {
1886-
lastValue = value;
1887-
if (isFunction(listener)) {
1888-
listener(value, old, scope);
1889-
}
1890-
if (isAllDefined(value)) {
1891-
scope.$$postDigest(function() {
1892-
if (isAllDefined(lastValue)) unwatch();
1893-
});
1894-
}
1895-
}, objectEquality);
1896-
1897-
return unwatch;
1898-
18991882
function isAllDefined(value) {
19001883
var allDefined = true;
19011884
forEach(value, function(val) {
19021885
if (!isDefined(val)) allDefined = false;
19031886
});
19041887
return allDefined;
19051888
}
1906-
}
19071889

19081890
function constantWatchDelegate(scope, listener, objectEquality, parsedExpression) {
19091891
var unwatch = scope.$watch(function constantWatch(scope) {
@@ -1918,22 +1900,28 @@ function $ParseProvider() {
19181900
var watchDelegate = parsedExpression.$$watchDelegate;
19191901
var useInputs = false;
19201902

1921-
var regularWatch =
1922-
watchDelegate !== oneTimeLiteralWatchDelegate &&
1923-
watchDelegate !== oneTimeWatchDelegate;
1903+
var isDone = parsedExpression.literal ? isAllDefined : isDefined;
19241904

1925-
var fn = regularWatch ? function regularInterceptedExpression(scope, locals, assign, inputs) {
1905+
function regularInterceptedExpression(scope, locals, assign, inputs) {
19261906
var value = useInputs && inputs ? inputs[0] : parsedExpression(scope, locals, assign, inputs);
19271907
return interceptorFn(value, scope, locals);
1928-
} : function oneTimeInterceptedExpression(scope, locals, assign, inputs) {
1929-
var value = parsedExpression(scope, locals, assign, inputs);
1908+
}
1909+
1910+
function oneTimeInterceptedExpression(scope, locals, assign, inputs) {
1911+
var value = useInputs && inputs ? inputs[0] : parsedExpression(scope, locals, assign, inputs);
19301912
var result = interceptorFn(value, scope, locals);
19311913
// we only return the interceptor's result if the
19321914
// initial value is defined (for bind-once)
1933-
return isDefined(value) ? result : value;
1934-
};
1915+
return isDone(value) ? result : value;
1916+
}
1917+
1918+
var fn = parsedExpression.oneTime ? oneTimeInterceptedExpression : regularInterceptedExpression;
1919+
1920+
// Propogate the literal/oneTime attributes
1921+
fn.literal = parsedExpression.literal;
1922+
fn.oneTime = parsedExpression.oneTime;
19351923

1936-
// Propagate $$watchDelegates other then inputsWatchDelegate
1924+
// Propagate or create inputs / $$watchDelegates
19371925
useInputs = !parsedExpression.inputs;
19381926
if (watchDelegate && watchDelegate !== inputsWatchDelegate) {
19391927
fn.$$watchDelegate = watchDelegate;

test/ng/parseSpec.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -2688,9 +2688,11 @@ describe('parser', function() {
26882688
expect($parse(':: ').literal).toBe(true);
26892689
}));
26902690

2691+
[true, false].forEach(function(isDeep) {
2692+
describe(isDeep ? 'deepWatch' : 'watch', function() {
26912693
it('should only become stable when all the properties of an object have defined values', inject(function($parse, $rootScope, log) {
26922694
var fn = $parse('::{foo: foo, bar: bar}');
2693-
$rootScope.$watch(fn, function(value) { log(value); }, true);
2695+
$rootScope.$watch(fn, function(value) { log(value); }, isDeep);
26942696

26952697
expect(log.empty()).toEqual([]);
26962698
expect($rootScope.$$watchers.length).toBe(1);
@@ -2718,7 +2720,7 @@ describe('parser', function() {
27182720

27192721
it('should only become stable when all the elements of an array have defined values', inject(function($parse, $rootScope, log) {
27202722
var fn = $parse('::[foo,bar]');
2721-
$rootScope.$watch(fn, function(value) { log(value); }, true);
2723+
$rootScope.$watch(fn, function(value) { log(value); }, isDeep);
27222724

27232725
expect(log.empty()).toEqual([]);
27242726
expect($rootScope.$$watchers.length).toBe(1);
@@ -2746,7 +2748,7 @@ describe('parser', function() {
27462748

27472749
it('should only become stable when all the elements of an array have defined values at the end of a $digest', inject(function($parse, $rootScope, log) {
27482750
var fn = $parse('::[foo]');
2749-
$rootScope.$watch(fn, function(value) { log(value); }, true);
2751+
$rootScope.$watch(fn, function(value) { log(value); }, isDeep);
27502752
$rootScope.$watch('foo', function() { if ($rootScope.foo === 'bar') {$rootScope.foo = undefined; } });
27512753

27522754
$rootScope.foo = 'bar';
@@ -2766,6 +2768,8 @@ describe('parser', function() {
27662768
}));
27672769
});
27682770
});
2771+
});
2772+
});
27692773

27702774

27712775
describe('watched $parse expressions', function() {

0 commit comments

Comments
 (0)