/
traverse.ts
65 lines (63 loc) 路 1.76 KB
/
traverse.ts
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
import { ProxyRaw, RawNode } from './environment'
import { isObservable, isSupportObservable } from './externals'
import { INode, IVisitor } from './types'
export const buildTreeNode = ({
target,
value,
key,
traverse,
shallow,
}: IVisitor) => {
const raw = ProxyRaw.get(value) || value
const parentRaw = ProxyRaw.get(target) || target
const parentNode = RawNode.get(parentRaw)
const currentNode = RawNode.get(raw)
if (currentNode) return currentNode
if (parentNode) {
const node: INode = {
path: parentNode.path.concat(key as any),
parent: parentNode,
observers: new Set(),
deepObservers: new Set(),
shallow: shallow || parentNode.shallow,
traverse: traverse || parentNode.traverse,
}
RawNode.set(value, node)
return node
} else {
const node: INode = {
path: [],
observers: new Set(),
deepObservers: new Set(),
shallow,
traverse,
}
RawNode.set(value, node)
return node
}
}
export const traverseIn = (target: any, key: PropertyKey, value: any) => {
if (isObservable(value)) return value
const parent = ProxyRaw.get(target) || target
const raw = ProxyRaw.get(value) || value
const parentNode = RawNode.get(parent)
const node = RawNode.get(raw)
if (parentNode) {
if (!isSupportObservable(value)) return value
const path = parentNode.path.concat(key as any)
const shallow = parentNode.shallow
if (!node) {
RawNode.set(raw, {
path,
parent: parentNode,
observers: new Set(),
deepObservers: new Set(),
shallow: parentNode.shallow,
traverse: parentNode.traverse,
})
}
if (shallow) return value
return parentNode.traverse({ target, key, value, path, shallow })
}
return value
}