-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathwatch.js
41 lines (38 loc) · 1.3 KB
/
watch.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
const getGetterWatchedByArray = computedAsyncProperty =>
function getter() {
for (let key of computedAsyncProperty.watch) {
if (typeof key !== 'string') throw new Error('AsyncComputed: watch elemnts must be strings')
// check if nested key is watched
const splittedByDot = key.split('.');
if (splittedByDot.length === 1) {
// if not just access it
this[key]
} else {
// access nested propety
try {
var start = this
for (let part of splittedByDot) {
start = start[part]
}
} catch (error) {
console.error('computedAsyncPlugin: bad path: ', key)
throw error
}
}
}
return computedAsyncProperty.get.call(this)
}
const getGetterWatchedByFunction = computedAsyncProperty =>
function getter() {
computedAsyncProperty.watch.call(this)
return computedAsyncProperty.get.call(this)
}
export function getWatchedGetter(computedAsyncProperty) {
if (typeof computedAsyncProperty.watch === 'function') {
return getGetterWatchedByFunction(computedAsyncProperty)
} else if (Array.isArray(computedAsyncProperty.watch)) {
return getGetterWatchedByArray(computedAsyncProperty)
} else {
throw Error('AsyncComouted: watch should be function or an array')
}
}