-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
432 lines (372 loc) Β· 11.6 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
let NIL = void 0,
TEXT = 3,
ELEMENT = 1,
COMPONENT = 2,
STATEFUL = 4,
RETAIN_KEY = '=',
REDRAWS = [],
EMPTY_OBJ = {},
isArray = Array.isArray,
isStr = x => typeof x === 'string',
isFn = x => typeof x === 'function',
isObj = x => x !== null && !isArray(x) && typeof x === 'object';
let getKey = v => v == null ? v : v.key;
let styles = obj =>
Object.entries(obj).map(
([k, v]) => k.replace(/[A-Z]/g, m => '-' + m.toLowerCase()) + ':' + v
).join(';');
let patchProp = (node, name, newProp, redraw) => {
if (name === 'dom') {
// do nothing
} else if (
name in node
&& name !== 'list'
&& name !== 'form'
&& name !== 'style'
) {
if (name[0] === 'o' && name[1] === 'n') {
let res, fn = newProp;
newProp = ev =>
(res = fn(ev)) instanceof Promise
? res.finally(_ => (redraw(), res = NIL))
: (redraw(), res = NIL);
}
node[name] = newProp;
} else if (newProp == null || newProp === false) {
node.removeAttribute(name);
} else {
node.setAttribute(name, name === 'style' && isObj(newProp) ? styles(newProp) : newProp);
}
};
let normalizeVnode = vnode =>
vnode !== true && vnode !== false && vnode
? vnode
: { type: TEXT, tag: '' };
let propsChanged = (a, b) => {
for (let k in a) if (a[k] !== b[k]) return true;
for (let k in b) if (a[k] !== b[k]) return true;
};
let createInstance = (vnode, oldVNode) => {
let instance,
memo = vnode.tag.memo,
oldProps = oldVNode ? oldVNode.props : NIL,
newProps = vnode.props;
if (oldProps && memo && !propsChanged(oldProps, newProps))
instance = oldVNode.instance;
else
instance = vnode.tag({ ...newProps, children: vnode.children }, oldProps || EMPTY_OBJ);
if (instance.tag === RETAIN_KEY)
instance = oldVNode.instance;
else if (isFn(instance)) {
vnode.type = STATEFUL;
instance.memo = memo;
instance = {
...vnode,
type: COMPONENT,
tag: instance
};
}
instance.key = vnode.key;
return instance;
};
let createComponent = (vnode, redraw) =>
createNode((vnode.instance = createInstance(vnode)), redraw);
let createNode = (vnode, redraw) => {
if (vnode.type === COMPONENT || vnode.type === STATEFUL)
return (vnode.node = createComponent(vnode, redraw));
let i,
props = vnode.props,
node = vnode.type === TEXT
? document.createTextNode(vnode.tag)
: document.createElement(vnode.tag);
for (i in props) patchProp(node, i, props[i], redraw);
if (vnode.type === ELEMENT)
for (i = 0; i < vnode.children.length; i++)
node.appendChild(
createNode(
(vnode.children[i] = normalizeVnode(vnode.children[i])),
redraw
)
);
vnode.rm = props
? isFn(props.dom) && props.dom(node)
: NIL;
return (vnode.node = node);
};
let getRemoves = (vnode, removes = []) => {
if (vnode.children !== NIL)
for (let i = 0; i < vnode.children.length; i++)
getRemoves(vnode.children[i], removes);
if (vnode.instance !== NIL)
getRemoves(vnode.instance, removes);
else if (isFn(vnode.rm)) {
let res = vnode.rm(vnode.node);
if (res && isFn(res.then)) removes._async = true;
removes.push(res);
}
return removes;
};
let removeChild = (parentNode, vnode) => {
let done = _ => parentNode.removeChild(vnode.node),
removes = getRemoves(vnode);
removes._async
? Promise.all(removes).finally(done)
: done();
};
let patch = (parent, node, oldVNode, newVNode, redraw) => {
if (oldVNode != null && oldVNode.tag === newVNode.tag)
newVNode.rm = oldVNode.rm;
if (oldVNode === newVNode) {
// memoized
} else if (oldVNode != null && oldVNode.type === TEXT && newVNode.type === TEXT) {
// they are both text nodes
// update if the newVNode does not equal the old one
if (oldVNode.tag !== newVNode.tag) node.nodeValue = newVNode.tag;
} else if (oldVNode == null || oldVNode.tag !== newVNode.tag) {
// if there is a tag mismatch
// or the old vnode does not exist
// there needs to be a node replacement
node = parent.insertBefore(
createNode((newVNode = normalizeVnode(newVNode)), redraw),
node
);
// if the oldVnode did exist, make sure to remove its real node from the real DOM
if (oldVNode != null) removeChild(parent, oldVNode);
} else if (oldVNode.type === STATEFUL && oldVNode.tag === newVNode.tag) {
newVNode.type = STATEFUL;
newVNode.instance = {
...oldVNode.instance,
props: newVNode.props,
children: newVNode.children
};
patch(parent, node, oldVNode.instance, newVNode.instance, redraw);
} else if (oldVNode.type === COMPONENT && oldVNode.tag === newVNode.tag) {
newVNode.instance = createInstance(newVNode, oldVNode);
patch(parent, node, oldVNode.instance, newVNode.instance, redraw);
} else {
let tmpVKid,
oldVKid,
oldKey,
newKey,
oldProps = oldVNode.props,
newProps = newVNode.props,
oldVKids = oldVNode.children,
newVKids = newVNode.children,
oldHead = 0,
newHead = 0,
oldTail = oldVKids.length - 1,
newTail = newVKids.length - 1;
for (let i in { ...oldProps, ...newProps }) {
if (
(i === "value" || i === "selected" || i === "checked"
? node[i]
: oldProps[i]) !== newProps[i]
) {
patchProp(node, i, newProps[i], redraw);
}
}
// patch children from top to bottom
while (newHead <= newTail && oldHead <= oldTail) {
if (
(oldKey = getKey(oldVKids[oldHead])) == null ||
oldKey !== getKey(newVKids[newHead])
) {
break;
}
patch(
node,
oldVKids[oldHead].node,
oldVKids[oldHead],
(newVKids[newHead] = normalizeVnode(
newVKids[newHead++],
oldVKids[oldHead++]
)),
redraw
);
}
// patch children from bottom to top
while (newHead <= newTail && oldHead <= oldTail) {
if (
(oldKey = getKey(oldVKids[oldTail])) == null ||
oldKey !== getKey(newVKids[newTail])
) {
break;
}
patch(
node,
oldVKids[oldTail].node,
oldVKids[oldTail],
(newVKids[newTail] = normalizeVnode(
newVKids[newTail--],
oldVKids[oldTail--]
)),
redraw
);
}
if (oldHead > oldTail) {
while (newHead <= newTail) {
node.insertBefore(
createNode(
(newVKids[newHead] = normalizeVnode(newVKids[newHead++])),
redraw
),
(oldVKid = oldVKids[oldHead]) && oldVKid.node
);
}
} else if (newHead > newTail) {
while (oldHead <= oldTail) {
removeChild(node, oldVKids[oldHead++]);
}
} else {
// grab all the old keys from the old children
let keyed = {},
newKeyed = {};
for (let i = oldHead; i <= oldTail; i++) {
if ((oldKey = getKey(oldVKids[i])) != null) {
keyed[oldKey] = oldVKids[i];
}
}
while (newHead <= newTail) {
oldKey = getKey((oldVKid = oldVKids[oldHead]));
newKey = getKey(
(newVKids[newHead] = normalizeVnode(newVKids[newHead], oldVKid))
);
// if we have this oldKey in newKeyed
// or if newKey is defined and is equal to the *next* old child's key
if (
newKeyed[oldKey] ||
(newKey != null && newKey === getKey(oldVKids[oldHead + 1]))
) {
// then you can remove the old child at the current oldHead index
// assuming it's key is null or undefined
if (oldKey == null) {
removeChild(node, oldVKid);
}
// then increment oldHead and go to next iteration
oldHead++;
continue;
}
// (remember, this is not a child, oldVNode is the PARENT during this call of patch)
if (newKey == null) {
if (oldKey == null) {
patch(
node,
oldVKid && oldVKid.node,
oldVKid,
newVKids[newHead],
redraw
);
newHead++;
}
oldHead++;
} else {
if (oldKey === newKey) {
patch(
node,
oldVKid.node,
oldVKid,
newVKids[newHead],
redraw
)
newKeyed[newKey] = true;
oldHead++;
} else {
if ((tmpVKid = keyed[newKey]) != null) {
patch(
node,
node.insertBefore(tmpVKid.node, oldVKid && oldVKid.node),
tmpVKid,
newVKids[newHead],
redraw
);
newKeyed[newKey] = true;
} else {
patch(
node,
oldVKid && oldVKid.node,
null,
newVKids[newHead],
redraw
);
}
}
newHead++;
}
}
while (oldHead <= oldTail) {
if (getKey((oldVKid = oldVKids[oldHead++])) == null) {
removeChild(node, oldVKid);
}
}
for (let i in keyed) {
if (newKeyed[i] == null) {
removeChild(node, keyed[i]);
}
}
}
}
return (newVNode.node = node);
};
let addChildren = (x, children) => {
if (isArray(x)) for (let i = 0; i < x.length; i++) addChildren(x[i], children);
else if (isStr(x) || typeof x === 'number') children.push({ type: TEXT, tag: x });
else children.push(x);
};
export function mount(node, root) {
node.innerHTML = '<a></a>';
node = node.lastChild;
let dom = { type: ELEMENT, node },
draw = _ => (node = patch(
node.parentNode, // parentNode
node, // node
dom, // oldVnode
(dom = root()), // newVnode
redraw // redraw
)),
redraw = _ => requestAnimationFrame(draw);
REDRAWS.push(redraw);
return redraw() && redraw;
}
/** @type {import('./index.d.ts').redraw} **/
export const redraw = _ =>
REDRAWS.map(r => r());
/** @type {import('./index.d.ts').reset} **/
export const reset = _ => REDRAWS = [];
/** @type {import('./index.d.ts').memo} **/
export const memo = cmp => (cmp.memo = 1) && cmp;
/** @type {import('./index.d.ts').m} **/
export function m(tag, ...tail) {
let key, i, tmp, classes,
props = {},
children = [],
first = tail[0],
type = isFn(tag)
? COMPONENT
: ELEMENT;
// compensate for when jsx passes null for the first tail item
if (first == null) tail.shift();
// check if first child is props object
if (tail.length > 0 && isObj(first) && !isArray(first.children))
[{ key, ...props }, ...tail] = tail;
if (isStr(tag)) {
if (props.className)
props.class = props.className;
[tag, ...classes] = tag.split('.');
classes = classes.join(' ');
if (isObj(tmp = props.class)) {
for (i in tmp) {
if (tmp[i]) {
if (classes) classes += ' ';
classes += i;
}
}
}
if (isStr(tmp)) classes += !classes ? tmp : tmp ? ' ' + tmp : '';
if (classes) props.class = classes;
}
addChildren(tail, children); // will recurse through tail and push valid childs to `children`
return tag === '[' // jsx fragment
? children
: { type, tag, key, props, children };
}
m.retain = _ => m(RETAIN_KEY);