/
root.ts
75 lines (60 loc) · 1.77 KB
/
root.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
66
67
68
69
70
71
72
73
74
75
import { TaroElement } from './element'
import { NodeType } from './node_types'
import { MpInstance, HydratedData } from '../render'
import { UpdatePayload, UpdatePayloadValue } from './node'
import { isFunction, Shortcuts } from '@tarojs/shared'
export class TaroRootElement extends TaroElement {
private pendingUpdate = false
private updatePayloads: UpdatePayload[] = []
public ctx: null | MpInstance = null
public constructor () {
super(NodeType.ELEMENT_NODE, 'root')
}
public get _path () {
return 'root'
}
protected get _root () {
return this
}
public enqueueUpdate (payload: UpdatePayload) {
this.updatePayloads.push(payload)
if (this.pendingUpdate || this.ctx === null) {
return
}
this.performUpdate()
}
public performUpdate (initRender = false) {
this.pendingUpdate = true
const ctx = this.ctx!
setTimeout(() => {
const data: Record<string, UpdatePayloadValue | ReturnType<HydratedData>> = Object.create(null)
const resetPaths = new Set<string>(
initRender
? ['root.cn.[0]', 'root.cn[0]']
: []
)
while (this.updatePayloads.length > 0) {
const { path, value } = this.updatePayloads.shift()!
if (path.endsWith(Shortcuts.Childnodes)) {
resetPaths.add(path)
}
data[path] = value
}
for (const path in data) {
resetPaths.forEach(p => {
// 已经重置了数组,就不需要分别再设置了
if (path.includes(p) && path !== p) {
delete data[path]
}
})
const value = data[path]
if (isFunction(value)) {
data[path] = value()
}
}
ctx.setData(data, () => {
this.pendingUpdate = false
})
}, 1)
}
}