-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeps-parser.js
65 lines (59 loc) · 1.57 KB
/
deps-parser.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var Emitter = require('./emitter'),
utils = require('./utils'),
Observer = require('./observer'),
catcher = new Emitter()
/**
* Auto-extract the dependencies of a computed property
* by recording the getters triggered when evaluating it.
*/
function catchDeps (binding) {
if (binding.isFn) return
utils.log('\n- ' + binding.key)
var got = utils.hash()
binding.deps = []
catcher.on('get', function (dep) {
var has = got[dep.key]
if (
// avoid duplicate bindings
(has && has.compiler === dep.compiler) ||
// avoid repeated items as dependency
// only when the binding is from self or the parent chain
(dep.compiler.repeat && !isParentOf(dep.compiler, binding.compiler))
) {
return
}
got[dep.key] = dep
utils.log(' - ' + dep.key)
binding.deps.push(dep)
dep.subs.push(binding)
})
binding.value.$get()
catcher.off('get')
}
/**
* Test if A is a parent of or equals B
*/
function isParentOf (a, b) {
while (b) {
if (a === b) {
return true
}
b = b.parent
}
}
module.exports = {
/**
* the observer that catches events triggered by getters
*/
catcher: catcher,
/**
* parse a list of computed property bindings
*/
parse: function (bindings) {
utils.log('\nparsing dependencies...')
Observer.shouldGet = true
bindings.forEach(catchDeps)
Observer.shouldGet = false
utils.log('\ndone.')
}
}