forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.ts
More file actions
210 lines (180 loc) · 5.45 KB
/
component.ts
File metadata and controls
210 lines (180 loc) · 5.45 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { EventType } from 'overmind'
import { h } from 'snabbdom/h'
import { VNode, VNodeData } from 'snabbdom/vnode'
import { getApp, getName, patch } from './utils'
const SELF = 'self'
let nextId = 0
let nextComponentId = {}
let nextComponentInstanceId = {}
function copyToComponent(vnode: VNode, cnode: VNode, component) {
;(vnode.data as VNodeData).fn = (cnode.data as VNodeData).fn
cnode.text = vnode.text
cnode.elm = vnode.elm
cnode.children = vnode.children
cnode.data = {
...vnode.data,
on: {
...(vnode.data as any).on,
},
hook: {
...(vnode.data as any).hook,
prepatch,
postpatch,
destroy,
},
component,
}
component.currentNode = cnode
}
function init(componentNode: VNode): void {
try {
const component = (componentNode.data as VNodeData).component
const name = component.name
const tree = (component.tree = getApp().proxyStateTree.getTrackStateTree())
component.state = component.fn.getInitialState
? component.fn.getInitialState(component.props)
: {}
component.currentNode = componentNode
component.changeState = (newState) => {
Object.assign(component.state, newState)
patch(component.currentNode, {
...component.currentNode,
forceUpdate: true,
})
}
const createSelfNode = () => {
const node = (component.fn as any)(component.props, {
changeState: component.changeState,
state: component.state,
appState: tree.state,
actions: getApp().actions,
reaction: getApp().reaction,
})
if (node.sel !== SELF) {
throw new Error(
`You are not returning a <self /> element from the component ${name} `
)
}
return node
}
component.onFlush = (_, __, flushId) => {
patch(component.currentNode, {
...component.currentNode,
flushId: flushId,
})
}
tree.track(component.onFlush)
const newNode = createSelfNode()
tree.stopTracking()
copyToComponent(newNode, componentNode, component)
if (!(name in nextComponentId)) {
nextComponentId[name] = nextId++
}
if (!(name in nextComponentInstanceId)) {
nextComponentInstanceId[name] = 0
}
nextComponentInstanceId[name] = nextId++
const componentId = nextComponentId
const componentInstanceId = nextComponentInstanceId[name]
Object.assign(component, {
componentId,
componentInstanceId,
})
getApp().eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId,
componentInstanceId,
name,
paths: Array.from(tree.pathDependencies) as any,
})
} catch (error) {
console.log(error.message)
}
}
function prepatch(oldVnode: VNode, vnode: VNode): void {
const oldComponent = (oldVnode.data as VNodeData).component || {}
const newComponent = (vnode.data as VNodeData).component || {}
const oldProps = oldComponent.props || {}
const props = newComponent.props || {}
const oldPropsKeys = Object.keys(oldProps)
const propsKeys = Object.keys(props)
const overmind = {
changeState: oldComponent.changeState,
state: oldComponent.state,
appState: oldComponent.tree.state,
actions: getApp().actions,
reaction: getApp().reaction,
}
oldVnode.children = oldComponent.currentNode.children
if ((vnode as any).flushId || (vnode as any).forceUpdate) {
oldComponent.tree.track(oldComponent.onFlush)
const newNode = (newComponent.fn as any)(props, overmind)
oldComponent.tree.stopTracking()
copyToComponent(newNode, vnode, Object.assign(oldComponent, newComponent))
return
}
if (
oldComponent.fn !== newComponent.fn ||
oldPropsKeys.length !== propsKeys.length
) {
oldComponent.tree.track(oldComponent.onFlush)
const newNode = (newComponent.fn as any)(props, overmind)
oldComponent.tree.stopTracking()
copyToComponent(newNode, vnode, Object.assign(oldComponent, newComponent))
return
}
const keys = new Set<string>([...oldPropsKeys, ...propsKeys])
for (let key of keys) {
if (oldProps[key] !== props[key]) {
oldComponent.tree.track(oldComponent.onFlush)
const newNode = (newComponent.fn as any)(props, overmind)
oldComponent.tree.stopTracking()
copyToComponent(newNode, vnode, Object.assign(oldComponent, newComponent))
return
}
}
copyToComponent(oldVnode, vnode, Object.assign(oldComponent, newComponent))
}
function postpatch(_, nextNode) {
try {
const component = nextNode.data.component
if (nextNode.flushId) {
getApp().eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
componentId: component.componentId,
componentInstanceId: component.componentInstanceId,
name: component.name,
flushId: component.flushId,
paths: Array.from(component.tree.pathDependencies) as any,
})
}
} catch (error) {
console.log(error.message)
}
}
function destroy(node) {
try {
node.children = node.data.component.currentNode.children
getApp().proxyStateTree.disposeTree(node.data.component.tree)
} catch (error) {
console.log(error.message)
}
}
export const component = (component, props, children) => {
const name = getName(component.name)
return h(name, {
key: props ? props.key : undefined,
hook: {
init,
prepatch,
postpatch,
destroy,
},
component: {
fn: component,
name,
props: {
...props,
children: children.length ? children : null,
},
},
})
}