-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
200 lines (173 loc) · 5.85 KB
/
index.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
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
'use strict'
/**
* Glossary
*
* element -- representation returned by `React.createElement()`
*
* instance -- mutable state defined and managed by renderer
* end product of renderer activity, produced from elements
* in the DOM renderer, "instances" are actual DOM nodes
*
* component -- instance of Component or PureComponent, user-defined
* intermediary step on the way to instances
*
* container -- mutable host for component tree and scheduling, defined internally in React
*
* also called FiberRoot and OpaqueRoot
*
* in react-dom, the user-facing API pretends that
* the root DOM node is the root container,
* while secretly storing the actual container on that node
*
* root -- ancestral container
*
* container info -- container.containerInfo property, used by renderer to store custom state
*
* fiber -- holds elements, instance tree, component tree, schedules updates
*
*
* Information Flow
*
* elements -> fiber
* -> components
* -> instances -> container
*/
const {createElement} = require('react')
const {noop, slice, id, isFunction, isObject, isArray, isList, isString, validate} = require('fpx')
const {ReactFiberReconciler} = process.env.NODE_ENV === 'production'
? require('./build/react16/react-internals.production.min')
: require('./build/react16/react-internals.development')
const CONTAINER_INFO_INSTANCE_MARKER = 'RootContainerInfoInstance'
const ELEM_INSTANCE_MARKER = 'ElemInstance'
const TEXT_INSTANCE_MARKER = 'TextInstance'
exports.createContainer = createContainer
function createContainer() {
return internal.createContainer({$$typeof: CONTAINER_INFO_INSTANCE_MARKER, children: []})
}
exports.renderToContainer = renderToContainer
function renderToContainer(container, children, callback) {
validate(isContainer, container)
validate(isElementOrElements, children)
if (callback != null) validate(isFunction, callback)
const parentComponent = null
internal.updateContainer(children, container, parentComponent, callback)
}
exports.unmountAtContainer = unmountAtContainer
function unmountAtContainer(container) {
validate(isContainer, container)
const children = null
const parentComponent = null
internal.updateContainer(children, container, parentComponent)
}
exports.containerToElements = containerToElements
function containerToElements({containerInfo: {children}}) {
return children.map(instanceToElement)
}
const internal = exports.internal = ReactFiberReconciler({
createInstance,
createTextInstance,
getRootHostContext: noop,
getChildHostContext: noop,
getPublicInstance: id,
finalizeInitialChildren: noop,
prepareUpdate,
commitUpdate,
commitMount: noop,
commitTextUpdate,
shouldSetTextContent: False,
resetTextContent: noop,
shouldDeprioritizeSubtree: False,
appendChild,
appendInitialChild: appendChild,
appendChildToContainer: appendChild,
insertBefore,
insertInContainerBefore: insertBefore,
removeChild,
removeChildFromContainer: removeChild,
scheduleDeferredCallback,
prepareForCommit: noop,
resetAfterCommit: noop,
})
function createInstance(type, props) {
return {
$$typeof: ELEM_INSTANCE_MARKER,
type,
props: propsWithoutChildren(props),
children: [],
}
}
function createTextInstance(text) {
return {$$typeof: TEXT_INSTANCE_MARKER, text}
}
function prepareUpdate(elemInstance, type, oldProps, newProps) {
return propsWithoutChildren(newProps)
}
function commitUpdate(elemInstance, preparedUpdatePayload) {
elemInstance.props = preparedUpdatePayload
}
function commitTextUpdate(textInstance, oldText, newText) {
textInstance.text = newText
}
function appendChild(parentInstance, childInstance) {
const {children} = parentInstance
const index = children.indexOf(childInstance)
if (index !== -1) children.splice(index, 1)
children.push(childInstance)
}
function insertBefore(parentInstance, childInstance, beforeChildInstance) {
const {children} = parentInstance
const index = children.indexOf(childInstance)
if (index !== -1) children.splice(index, 1)
const beforeIndex = children.indexOf(beforeChildInstance)
if (beforeIndex === -1) throw Error('This child does not exist')
children.splice(beforeIndex, 0, childInstance)
}
function removeChild(parentInstance, childInstance) {
const {children} = parentInstance
const index = children.indexOf(childInstance)
if (index === -1) throw Error('This child does not exist')
children.splice(index, 1)
}
function scheduleDeferredCallback(callback) {
process.nextTick(callback, deadline)
}
const deadline = {timeRemaining() {return Infinity}}
function False() {
return false
}
function isContainer(value) {
return isObject(value) && 'containerInfo' in value
}
function isElementOrElements(value) {
return isElement(value) || (isArray(value) && value.every(isElementOrElements))
}
function isElement(value) {
return (
value == null ||
isString(value) ||
(isObject(value) && value.$$typeof === Symbol.for('react.element'))
)
}
function propsWithoutChildren(fullProps) {
const props = {}
for (const key in fullProps) {
if (key !== 'children') props[key] = fullProps[key]
}
return props
}
function isElemInstance(value) {
return isObject(value) && value.$$typeof === ELEM_INSTANCE_MARKER
}
function isTextInstance(value) {
return isObject(value) && value.$$typeof === TEXT_INSTANCE_MARKER
}
function instanceToElement(value) {
return isElemInstance(value)
? createElement(value.type, value.props, ...toArray(value.children).map(instanceToElement))
: isTextInstance(value)
? value.text
: null
}
function toArray(value) {
return isArray(value) ? value : isList(value) ? slice(value) : value == null ? [] : [value]
}