-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrenderer.js
More file actions
196 lines (136 loc) · 4.11 KB
/
Copy pathrenderer.js
File metadata and controls
196 lines (136 loc) · 4.11 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
const Reconciler = require('react-reconciler')
const { Components, hasComponent } = require('./components')
const { emptyObject, now, scheduleDeferredCallback } = require('./utils')
function appendInitialChild(parentInstance, child) {
// TODO: views that do not support addChildView
parentInstance.addChildView(child)
}
function createInstance(type, props, internalInstanceHandle) {
if (!hasComponent(type)) {
throw new Error(`receive invalid type: ${type}`)
}
// console.error('internalInstanceHandle', internalInstanceHandle)
const Comp = Components[type]
return new Comp(props)
}
// eslint-disable-next-line
function finalizeInitialChildren(docElement, type, props) {
// console.log('finalizeInitialChildren')
return false
}
function getPublicInstance(instance) {
return instance._ele
}
function prepareForCommit() {
// console.log('getPublicInstance')
}
function prepareUpdate() {
// always update
return true
}
function resetAfterCommit() {}
function getRootHostContext() {
return emptyObject
}
function getChildHostContext() {
return emptyObject
}
// TODO: utilize the text content
function shouldSetTextContent() {
return false
}
function resetTextContent() {}
function createTextInstance() {
return null
}
// eslint-disable-next-line
function shouldDeprioritizeSubtree(type, props) {
return false
}
// Mutation
function appendChild(parentInstance, child) {
if (typeof parentInstance.addChildView !== 'function') {
throw new Error('expect the instance to be an gui container')
}
parentInstance.addChildView(child)
}
function appendChildToContainer(parentInstance, child) {
if (typeof parentInstance.addChildView !== 'function') {
throw new Error('expect the instance to be an gui container')
}
parentInstance.addChildView(child.getElement())
}
function removeChild(parentInstance, child) {
parentInstance.removeChildView(child)
}
function removeChildFromContainer(parentInstance, child) {
parentInstance.removeChildView(child)
}
function insertBefore(parentInstance, child, beforeChild) {
parentInstance.insertBefore(child, beforeChild)
}
function insertInContainerBefore(parentInstance, child, beforeChild) {
parentInstance.insertBefore(child, beforeChild)
}
// TODO: what's the "updatePayload"?
function commitUpdate(instance, updatePayload, type, oldProps, newProps) {
// console.log('instance, updatePayload', instance, updatePayload, type, oldProps, newProps)
instance.update(oldProps, newProps)
}
function commitMount(instance, updatePayload, type, oldProps, newProps) {
// console.log('commitMount')
}
function commitTextUpdate(textInstance, oldText, newText) {
// console.log('commitTextUpdate')
}
export const YueRenderer = Reconciler({
appendInitialChild,
createInstance,
createTextInstance,
finalizeInitialChildren,
getPublicInstance,
prepareForCommit,
prepareUpdate,
resetAfterCommit,
resetTextContent,
getRootHostContext,
getChildHostContext,
shouldSetTextContent,
scheduleDeferredCallback,
shouldDeprioritizeSubtree,
now,
supportsMutation: true,
appendChild,
appendChildToContainer,
commitMount,
commitUpdate,
insertBefore,
insertInContainerBefore,
removeChild,
removeChildFromContainer,
resetTextContent,
commitTextUpdate
})
function render(element, guiContainer, callback) {
let root = guiContainer._reactRootContainer
if (!root) {
// Remove all children of the guiContainer
const childCount = guiContainer.childCount()
for (let i = 0; i < childCount; i += 1) {
guiContainer.removeChildView(guiContainer.childAt(0))
}
const newRoot = YueRenderer.createContainer(guiContainer)
// eslint-disable-next-line
root = guiContainer._reactRootContainer = newRoot
}
YueRenderer.injectIntoDevTools({
bundleType: 1, // 0 for PROD, 1 for DEV
version: require('../package.json').version, // version for your renderer
rendererPackageName: 'react-yue', // package name
findHostInstanceByFiber: YueRenderer.findHostInstance, // host instance (root)
})
return YueRenderer.updateContainer(element, root, null, callback)
}
module.exports = {
render,
}