-
Notifications
You must be signed in to change notification settings - Fork 12
/
fre.js
482 lines (473 loc) · 12.9 KB
/
fre.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
'use strict';
// Object.defineProperty(exports, '__esModule', { value: true });
const updateElement = (dom, oldProps, newProps) => {
for (let name in Object.assign(Object.assign({}, oldProps), newProps)) {
let oldValue = oldProps[name];
let newValue = newProps[name];
if (oldValue == newValue || name === 'children');
else if (name === 'style') {
for (const k in Object.assign(Object.assign({}, oldValue), newValue)) {
if (!(oldValue && newValue && oldValue[k] === newValue[k])) {
dom[name][k] = (newValue && newValue[k]) || '';
}
}
}
else if (name[0] === 'o' && name[1] === 'n') {
name = name.slice(2).toLowerCase();
if (oldValue)
dom.removeEventListener(name, oldValue);
dom.addEventListener(name, newValue);
}
else if (name in dom && !(dom instanceof SVGElement)) {
dom[name] = newValue == null ? '' : newValue;
}
else if (newValue == null || newValue === false) {
dom.removeAttribute(name);
}
else {
dom.setAttribute(name, newValue);
}
}
};
const createElement = (fiber) => {
const dom = fiber.type === 'text'
? document.createTextNode('')
: fiber.tag === 4
? document.createElementNS('http://www.w3.org/2000/svg', fiber.type)
: document.createElement(fiber.type);
updateElement(dom, {}, fiber.props);
return dom;
};
let cursor = 0;
const resetCursor = () => {
cursor = 0;
};
const useState = (initState) => {
return useReducer(null, initState);
};
const useReducer = (reducer, initState) => {
const [hook, current] = getHook(cursor++);
const setter = (value) => {
let newValue = reducer
? reducer(hook[0], value)
: isFn(value)
? value(hook[0])
: value;
if (newValue !== hook[0]) {
hook[0] = newValue;
scheduleWork(current);
}
};
if (hook.length) {
return [hook[0], setter];
}
else {
hook[0] = initState;
return [initState, setter];
}
};
const useEffect = (cb, deps) => {
return effectImpl(cb, deps, 'effect');
};
const useLayout = (cb, deps) => {
return effectImpl(cb, deps, 'layout');
};
const effectImpl = (cb, deps, key) => {
let [hook, current] = getHook(cursor++);
if (isChanged(hook[1], deps)) {
hook[0] = useCallback(cb, deps);
hook[1] = deps;
current.hooks[key].push(hook);
}
};
const useMemo = (cb, deps) => {
let hook = getHook(cursor++)[0];
if (isChanged(hook[1], deps)) {
hook[1] = deps;
return (hook[0] = cb());
}
return hook[0];
};
const useCallback = (cb, deps) => {
return useMemo(() => cb, deps);
};
const useRef = (current) => {
return useMemo(() => ({ current }), []);
};
const getHook = (cursor) => {
const current = getCurrentFiber();
let hooks = current.hooks || (current.hooks = { list: [], effect: [], layout: [] });
if (cursor >= hooks.list.length) {
hooks.list.push([]);
}
return [hooks.list[cursor], current];
};
const isChanged = (a, b) => {
return !a || b.some((arg, index) => arg !== a[index]);
};
let taskQueue = [];
let currentCallback;
let frameDeadline = 0;
const frameLength = 5;
const scheduleCallback = (callback) => {
const currentTime = getTime();
const timeout = 3000;
const dueTime = currentTime + timeout;
let newTask = {
callback,
dueTime
};
taskQueue.push(newTask);
currentCallback = flush;
planWork(null);
};
const flush = (iniTime) => {
let currentTime = iniTime;
let currentTask = peek(taskQueue);
while (currentTask) {
const timeout = currentTask.dueTime <= currentTime;
if (!timeout && shouldYeild())
break;
let callback = currentTask.callback;
currentTask.callback = null;
let next = isFn(callback) && callback(timeout);
next ? (currentTask.callback = next) : taskQueue.shift();
currentTask = peek(taskQueue);
currentTime = getTime();
}
return !!currentTask;
};
const peek = (queue) => {
queue.sort((a, b) => a.dueTime - b.dueTime);
return queue[0];
};
const flushWork = () => {
if (isFn(currentCallback)) {
let currentTime = getTime();
frameDeadline = currentTime + frameLength;
let more = currentCallback(currentTime);
more ? planWork(null) : (currentCallback = null);
}
};
const planWork = (() => {
if (typeof MessageChannel !== 'undefined') {
const { port1, port2 } = new MessageChannel();
port1.onmessage = flushWork;
return (cb) => cb ? requestAnimationFrame(cb) : port2.postMessage(null);
}
return (cb) => setTimeout(cb || flushWork);
})();
const shouldYeild = () => {
return getTime() >= frameDeadline;
};
const getTime = () => performance.now();
const options = {
catchError (_, e) {
throw e;
}
};
let preCommit;
let currentFiber;
let WIP;
let updateQueue = [];
let commitQueue = [];
const render = (vnode, node, done) => {
let rootFiber = {
node,
props: { children: vnode },
done
};
scheduleWork(rootFiber);
};
const scheduleWork = (fiber) => {
if (!fiber.dirty) {
fiber.dirty = true;
updateQueue.push(fiber);
}
scheduleCallback(reconcileWork);
};
const reconcileWork = (timeout) => {
if (!WIP)
WIP = updateQueue.shift();
while (WIP && (!shouldYeild() || timeout)) {
WIP = reconcile(WIP);
}
if (WIP && !timeout) {
return reconcileWork.bind(null);
}
if (preCommit)
commitWork(preCommit);
return null;
};
const reconcile = (WIP) => {
WIP.parentNode = getParentNode(WIP);
if (isFn(WIP.type)) {
try {
updateHook(WIP);
}
catch (e) {
options.catchError(WIP, e);
}
}
else {
updateHost(WIP);
}
WIP.dirty = WIP.dirty ? false : 0;
commitQueue.push(WIP);
if (WIP.child)
return WIP.child;
while (WIP) {
if (!preCommit && WIP.dirty === false) {
preCommit = WIP;
return null;
}
if (WIP.sibling) {
return WIP.sibling;
}
WIP = WIP.parent;
}
};
const updateHook = (WIP) => {
currentFiber = WIP;
resetCursor();
let children = WIP.type(WIP.props);
if (isStr(children))
children = createText(children);
reconcileChildren(WIP, children);
};
const updateHost = (WIP) => {
if (!WIP.node) {
if (WIP.type === 'svg') {
WIP.tag = 4;
}
WIP.node = createElement(WIP);
}
let p = WIP.parentNode || {};
WIP.insertPoint = p.last || null;
p.last = WIP;
WIP.node.last = null;
reconcileChildren(WIP, WIP.props.children);
};
const getParentNode = (WIP) => {
while ((WIP = WIP.parent)) {
if (!isFn(WIP.type))
return WIP.node;
}
};
const reconcileChildren = (WIP, children) => {
delete WIP.child;
const oldFibers = WIP.kids;
const newFibers = (WIP.kids = hashfy(children));
let reused = {};
for (const k in oldFibers) {
let newFiber = newFibers[k];
let oldFiber = oldFibers[k];
if (newFiber && newFiber.type === oldFiber.type) {
reused[k] = oldFiber;
}
else {
oldFiber.op = 3;
commitQueue.push(oldFiber);
}
}
let prevFiber;
for (const k in newFibers) {
let newFiber = newFibers[k];
let oldFiber = reused[k];
if (oldFiber) {
oldFiber.op = 2;
newFiber = Object.assign(Object.assign({}, oldFiber), newFiber);
newFiber.lastProps = oldFiber.props;
if (shouldPlace(newFiber))
newFiber.op = 1;
}
else {
newFiber.op = 1;
}
newFibers[k] = newFiber;
newFiber.parent = WIP;
if (prevFiber) {
prevFiber.sibling = newFiber;
}
else {
if (WIP.tag === 4) {
newFiber.tag = 4;
}
WIP.child = newFiber;
}
prevFiber = newFiber;
}
if (prevFiber)
prevFiber.sibling = null;
};
const shouldPlace = (fiber) => {
let p = fiber.parent;
if (isFn(p.type))
return p.key && !p.dirty;
return fiber.key;
};
const commitWork = (fiber) => {
commitQueue.forEach(c => c.parent && commit(c));
fiber.done && fiber.done();
commitQueue.length = 0;
preCommit = null;
WIP = null;
};
const commit = (fiber) => {
const { op, parentNode, node, ref, hooks } = fiber;
if (op === 3) {
hooks && hooks.list.forEach(cleanup);
cleanupRef(fiber.kids);
while (isFn(fiber.type))
fiber = fiber.child;
parentNode.removeChild(fiber.node);
}
else if (isFn(fiber.type)) {
if (hooks) {
side(hooks.layout);
planWork(() => side(hooks.effect));
}
}
else if (op === 2) {
updateElement(node, fiber.lastProps, fiber.props);
}
else {
let point = fiber.insertPoint ? fiber.insertPoint.node : null;
let after = point ? point.nextSibling : parentNode.firstChild;
if (after === node)
return;
if (after === null && node === parentNode.lastChild)
return;
parentNode.insertBefore(node, after);
}
refer(ref, node);
};
const hashfy = (c) => {
const out = {};
isArr(c)
? c.forEach((v, i) => isArr(v)
? v.forEach((vi, j) => (out[hs(i, j, vi.key)] = vi))
: some(v) && (out[hs(i, null, v.key)] = v))
: some(c) && (out[hs(0, null, c.key)] = c);
return out;
};
const refer = (ref, dom) => {
if (ref)
isFn(ref) ? ref(dom) : (ref.current = dom);
};
const cleanupRef = (kids) => {
for (const k in kids) {
const kid = kids[k];
refer(kid.ref, null);
if (kid.kids)
cleanupRef(kid.kids);
}
};
const side = (effects) => {
effects.forEach(cleanup);
effects.forEach(effect);
effects.length = 0;
};
const getCurrentFiber = () => currentFiber || null;
const effect = (e) => e[2] = e[0](currentFiber);
const cleanup = (e) => e[2] && e[2](currentFiber);
const isFn = (x) => typeof x === 'function';
const isStr = (s) => typeof s === 'number' || typeof s === 'string';
const some = (v) => v != null && v !== false && v !== true;
const hs = (i, j, k) => k != null && j != null
? '.' + i + '.' + k
: j != null
? '.' + i + '.' + j
: k != null
? '.' + k
: '.' + i;
const h = function (type, attrs) {
let props = attrs || {};
let key = props.key || null;
let ref = props.ref || null;
let children = [];
let simpleNode = '';
const childrenArgLen = arguments.length;
for (let i = 2; i < childrenArgLen; i++) {
let child = arguments[i];
const isEnd = i === childrenArgLen - 1;
while (isArr(child) && child.some(v => isArr(v))) {
child = [].concat(...child);
}
let vnode = some(child) ? child : '';
const isStrNode = isStr(vnode);
if (isStrNode) {
simpleNode += String(vnode);
}
if (simpleNode && (!isStrNode || isEnd)) {
children.push(createText(simpleNode));
simpleNode = '';
}
if (!isStrNode) {
children.push(vnode);
}
}
if (children.length) {
props.children = children.length === 1 ? children[0] : children;
}
delete props.key;
delete props.ref;
return { type, props, key, ref };
};
function createText (vnode) {
return { type: 'text', props: { nodeValue: vnode } };
}
const Fragment = (props) => {
return props.children;
};
const isArr = Array.isArray;
const Fre = {
h,
createElement: h,
render,
scheduleWork,
useState,
useReducer,
useEffect,
useMemo,
useCallback,
useRef,
Fragment,
getCurrentFiber,
options
};
// exports.Fragment = Fragment;
// exports.createElement = h;
// exports.default = Fre;
// exports.getCurrentFiber = getCurrentFiber;
// exports.h = h;
// exports.options = options;
// exports.render = render;
// exports.scheduleWork = scheduleWork;
// exports.useCallback = useCallback;
// exports.useEffect = useEffect;
// exports.useLayout = useLayout;
// exports.useLayoutEffect = useLayout;
// exports.useMemo = useMemo;
// exports.useReducer = useReducer;
// exports.useRef = useRef;
// exports.useState = useState;
window['React'] = {
Fragment: Fragment,
createElement: h,
default: Fre,
getCurrentFiber: getCurrentFiber,
h: h,
options: options,
render: render,
scheduleWork: scheduleWork,
useCallback: useCallback,
useEffect: useEffect,
useLayout: useLayout,
useLayoutEffect: useLayout,
useMemo: useMemo,
useReducer: useReducer,
useRef: useRef,
useState: useState,
}
//# sourceMappingURL=fre.js.map