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

Commit f766698

Browse files
committed
fixup! perf(ngClass): avoid deep-watching (if possible) and unnecessary copies
1 parent dfe4307 commit f766698

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/ng/directive/ngClass.js

+19-14
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,7 @@ function classDirective(name, selector) {
3232
}
3333

3434
if (name !== 'ngClass') {
35-
scope.$watch('$index', function($index) {
36-
// eslint-disable-next-line no-bitwise
37-
var newModulo = $index & 1;
38-
39-
if (newModulo !== oldModulo) {
40-
if (newModulo === selector) {
41-
addClasses(oldClassString);
42-
} else {
43-
removeClasses(oldClassString);
44-
}
45-
46-
oldModulo = newModulo;
47-
}
48-
});
35+
scope.$watch('$index', ngClassIndexWatchAction);
4936
}
5037

5138
scope.$watch(watchExpression, watchAction, isOneTime);
@@ -89,6 +76,24 @@ function classDirective(name, selector) {
8976
return classesToUpdate.join(' ');
9077
}
9178

79+
function ngClassIndexWatchAction($index) {
80+
// eslint-disable-next-line no-bitwise
81+
var newModulo = $index & 1;
82+
83+
if (newModulo !== oldModulo) {
84+
// This watch-action should run before the `ngClass[OneTime]WatchAction()`, thus it
85+
// adds/removes `oldClassString`. If the `ngClass` expression has changed as well, the
86+
// `ngClass[OneTime]WatchAction()` will update the classes.
87+
if (newModulo === selector) {
88+
addClasses(oldClassString);
89+
} else {
90+
removeClasses(oldClassString);
91+
}
92+
93+
oldModulo = newModulo;
94+
}
95+
}
96+
9297
function ngClassOneTimeWatchAction(newClassValue) {
9398
var newClassString = toClassString(newClassValue);
9499

0 commit comments

Comments
 (0)