-
Notifications
You must be signed in to change notification settings - Fork 0
/
didact.js
280 lines (249 loc) · 6.71 KB
/
didact.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
function createElement(type, props, ...children) {
return {
type,
props: {
...props,
children: children.map(child =>
typeof child === "object"
? child
: createTextElement(child)
)
}
}
}
function createTextElement(text) {
return {
type: "TEXT_ELEMENT",
props: {
nodeValue: text,
children: []
}
}
}
function createDOMElement(fiber) {
// create element
const dom =
fiber.type === "TEXT_ELEMENT"
? document.createTextNode("")
: document.createElement(fiber.type)
// assign properties(including event listeners)
updateFiberDOM(dom, {}, fiber.props)
return dom
}
const Helpers = {
isEvent: key => key.startsWith("on"),
isProperty: key =>
key !== "children" && !Helpers.isEvent(key),
isNew: (prev, next) => key =>
prev[key] !== next[key],
wasRemoved: (prev, next) => key => !(key in next)
}
function updateFiberDOM(dom, prevProps, nextProps) {
Object.keys(prevProps)
.forEach( name => {
if (Helpers.isProperty(name)) {
// remove old property if its not on the new fiber
if(Helpers.wasRemoved(prevProps, nextProps)(name)) {
dom[name] = ""
}
} else if (Helpers.isEvent(name)) {
// remove old event listener if its not on the new fiber
if (!(name in nextProps)) {
if (Helpers.isNew(prevProps, nextProps)(name)) {
const eventType = name
.toLowerCase()
.substring(2)
dom.removeEventListener(
eventType,
prevProps[name]
)
}
}
}
})
Object.keys(nextProps)
.forEach(name => {
// add new property
if (Helpers.isProperty(name)) {
if (Helpers.isNew(prevProps, nextProps)(name)) {
dom[name] = nextProps[name]
}
} else if (Helpers.isEvent(name)) {
// add new event listener
if (Helpers.isNew(prevProps, nextProps)(name)) {
const eventType = name
.toLowerCase()
.substring(2)
dom.addEventListener(
eventType,
nextProps[name]
)
}
}
})
}
function commitFiberTree() {
deletions.forEach(commitFiberTreeToDOM)
commitFiberTreeToDOM(fiberTreeRoot.child)
// prepare for next render
previousFiberTree = fiberTreeRoot
fiberTreeRoot = null
}
function commitFiberTreeToDOM(fiber) {
if (!fiber) {
return
}
const domParent = fiber.parent.dom
if (fiber.dom != null) {
if (fiber.effectTag === "PLACEMENT") {
// for a fiber that's completely new
domParent.appendChild(fiber.dom)
} else if (fiber.effectTag === "UPDATE") {
// for a fiber that has a previous version
updateFiberDOM(
fiber.dom,
fiber.previous.props,
fiber.props
)
}
}
// for a fiber that's no longer needed
if (fiber.effectTag === "DELETION") {
domParent.removeChild(fiber.dom)
}
commitFiberTreeToDOM(fiber.child)
commitFiberTreeToDOM(fiber.sibling)
}
function render(element, container) {
// set fiber tree's root
fiberTreeRoot = {
dom: container,
props: {
children: [element]
},
previous: previousFiberTree
}
deletions = []
currentFiber = fiberTreeRoot
}
let fiberTreeRoot = null
let currentFiber = null
let deletions = null
let previousFiberTree = null
function constructFiberTree(deadline) {
let shouldYield = false
// if fiber tree's root is set, construct Fiber Tree fiber by fiber in the browser's idle time
while (currentFiber && !shouldYield) {
currentFiber = constructFiber(
currentFiber
)
shouldYield = deadline.timeRemaining() < 1
}
if (!currentFiber && fiberTreeRoot) {
// when there are no more fiber's to be constructed
commitFiberTree()
}
requestIdleCallback(constructFiberTree)
}
requestIdleCallback(constructFiberTree)
function constructFiber(fiber) {
// add dom node 📦, in other words set fiber's dom element
if (!fiber.dom) {
fiber.dom = createDOMElement(fiber)
}
// create child fibers
const children = fiber.props.children
reconcileChildren(fiber, children)
// return next fiber: update currentFiber.
if (fiber.child) {
return fiber.child
}
// otherwise, start moving towards the fiber root i.e. towards the 'parent'
let nextFiber = fiber
while(nextFiber) {
if (nextFiber.sibling) {
return nextFiber.sibling
}
nextFiber = nextFiber.parent
}
}
function reconcileChildren(wipFiber, children) {
let index = 0
// if the fiber had a previous version, then get its previous version's child
let oldFiber =
wipFiber.previous && wipFiber.previous.child
let prevSibling = null
while (
index < children.length ||
oldFiber != null
) {
const child = children[index]
let newFiber = null
const isOfSameType =
oldFiber &&
child &&
child.type === oldFiber.type
if (isOfSameType) {
// create new fiber which points to its previous version
newFiber = {
type: oldFiber.type,
props: child.props,
dom: oldFiber.dom,
parent: wipFiber,
previous: oldFiber,
effectTag: "UPDATE",
}
}
if (child && !isOfSameType) {
// create a completely new fiber
newFiber = {
type: child.type,
props: child.props,
dom: null,
parent: wipFiber,
previous: null,
effectTag: "PLACEMENT"
}
}
if (oldFiber && !isOfSameType) {
// if the pevious version is no longer needed align it for deletion
oldFiber.effectTag = "DELETION"
deletions.push(oldFiber)
// as its only comparing the type of fibers, it may leave out some oldfibers which need deletion
}
if (oldFiber) {
// remember: fibers on the stem point to their immediate sibling
// so the previous version of the next child is to going to be current child's previous version's sibling
oldFiber = oldFiber.sibling
}
if (index === 0) {
wipFiber.child = newFiber
} else if (child) {
// if this is not the first child, then the preceding child's fiber should point to the current child's fiber
prevSibling.sibling = newFiber
}
// note: prevSibling acts as a pointer(reference).
prevSibling = newFiber
index++
}
}
const Didact = {
createElement,
render
}
/** @jsx Didact.createElement */
const container = document.querySelector('#root');
const updateValue = event => {
rerender(event.target.value);
};
const rerender = value => {
// rerender on each update in value
const element = (
<div>
<input onInput={updateValue} value={value}/>
<h2>Hello {value}</h2>
</div>
)
Didact.render(element, container);
};
rerender("World!");