-
Notifications
You must be signed in to change notification settings - Fork 395
/
Copy pathnotifyPatternObservers.js
51 lines (39 loc) · 1.59 KB
/
notifyPatternObservers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import getPotentialWildcardMatches from 'utils/getPotentialWildcardMatches';
var lastKey = /[^\.]+$/;
// TODO split into two functions? i.e. one for the top-level call, one for the cascade
export default function notifyPatternObservers ( ractive, registeredKeypath, actualKeypath, isParentOfChangedKeypath, isTopLevelCall ) {
var i, patternObserver, children, child, key, childActualKeypath, potentialWildcardMatches, cascade;
// First, observers that match patterns at the same level
// or higher in the tree
i = ractive.viewmodel.patternObservers.length;
while ( i-- ) {
patternObserver = ractive.viewmodel.patternObservers[i];
if ( patternObserver.regex.test( actualKeypath ) ) {
patternObserver.update( actualKeypath );
}
}
if ( isParentOfChangedKeypath ) {
return;
}
// If the changed keypath is 'foo.bar', we need to see if there are
// any pattern observer dependants of keypaths below any of
// 'foo.bar', 'foo.*', '*.bar' or '*.*' (e.g. 'foo.bar.*' or 'foo.*.baz' )
cascade = function ( keypath ) {
if ( children = ractive.viewmodel.depsMap[ keypath ] ) {
i = children.length;
while ( i-- ) {
child = children[i]; // foo.*.baz
key = lastKey.exec( child )[0]; // 'baz'
childActualKeypath = actualKeypath ? actualKeypath + '.' + key : key; // 'foo.bar.baz'
notifyPatternObservers( ractive, child, childActualKeypath ); // ractive, 'foo.*.baz', 'foo.bar.baz'
}
}
};
if ( isTopLevelCall ) {
potentialWildcardMatches = getPotentialWildcardMatches( actualKeypath );
potentialWildcardMatches.forEach( cascade );
}
else {
cascade( registeredKeypath );
}
}