-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
162 lines (136 loc) · 4.47 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
import * as React from '../../node_modules/react';
export { DOMHandlers } from './dom-handlers';
export default React;
import {
getVDOMElement,
setCurrentVDOMElement,
createVDOMElement,
} from './vdom-helpers';
import { createHooks, useEffect, useState } from './hooks';
export { useEffect, useState };
import { getRenderableVDOMDiff } from './diff';
/*
VDOM with pointer idea
Given the DOM:
<div>
<span>
<strong>yo</strong>
Hello world!
</span>
<button>Test</button>
</div>
And a structure where we represent an element and its rendered children as a structure with an element and renderedChildren, for example
const VDOMforStrong = { element: { type: strong, props: {}}, renderedChildren: [{ element: 'yo' }]];
So we get the full picture of the DOM above as the following VDOM representation:
const VDOM = {
element: { type: div, props: {}},
renderedChildren: [
{
element: { type: span, props: {}},
renderedChildren: [
{
element: { type: strong, props: {}},
renderedChildren: [{ element: 'yo' }]
},
{
element: 'Hello world!',
}
]
}, {
element: { type: button, props: {}},
renderedChildren: [{ element: 'Test' }],
}
}
We can then create "pointer"s to access specific children based on their location in the tree, for example to access "Hello world!",
we need to dig into the first element's children picking the first child "span", then pick the second child from that element which is "Hello world!".
// So a pointer to it can look like:
const pointerToHelloWorld = [0, 1];
const element = getVDOMElement(pointerToHelloWorld, VDOM); // [`Hello world!`]
And you access the element itself by extracting taking the element from it, e.g.
const { element: button } = getVDOMElement([1]], VDOM); // button
*/
export const isPrimitiveElementFromJSX = element => typeof element !== 'object';
const renderComponentElement = (element, VDOM, VDOMPointer, hooks) => {
const {
props: { children, ...props },
type,
} = element;
const previousDOMElement = (getVDOMElement(VDOMPointer, VDOM.previous) || {})
.element;
const isFirstRender =
previousDOMElement === undefined ||
previousDOMElement.type !== element.type;
setCurrentVDOMElement(
VDOMPointer,
createVDOMElement(element, VDOMPointer),
VDOM,
);
if (typeof type === 'function') {
const FunctionalComponent = type;
hooks.registerHooks(VDOMPointer, isFirstRender);
const renderedElement = FunctionalComponent({ children, ...props });
const renderedElementDOM = render(
renderedElement,
VDOM,
[...VDOMPointer, 0],
hooks,
);
return renderedElementDOM;
}
if (typeof children !== 'undefined') {
const childrenArray = Array.isArray(children) ? children : [children];
const renderedChildren = childrenArray.map((child, index) =>
render(child, VDOM, [...VDOMPointer, index], hooks),
);
return {
...element,
props: {
...props,
children: renderedChildren,
},
VDOMPointer,
};
}
return { ...element, VDOMPointer };
};
const renderPrimitive = (value, VDOM, VDOMPointer) => {
const VDOMElement = createVDOMElement(value, VDOMPointer);
setCurrentVDOMElement(
VDOMPointer,
VDOMElement,
VDOM,
);
return VDOMElement.element;
};
const render = (element, VDOM, VDOMPointer, hooks) =>
isPrimitiveElementFromJSX(element)
? renderPrimitive(element, VDOM, VDOMPointer)
: renderComponentElement(element, VDOM, VDOMPointer, hooks);
const rootRender = (element, hooks, vdom) => {
let renderableVDOM = render(element, vdom, [], hooks);
return renderableVDOM;
};
// this function will call the updateCallback on every state change
// so the DOM is re-rendered
export const startRenderSubscription = (element, updateCallback) => {
let vdom = {
previous: {},
current: {},
};
let afterUpdate;
const registerOnUpdatedCallback = callback => {
afterUpdate = callback;
};
const update = hooks => {
const renderableVDOM = rootRender(element, hooks, vdom);
// We calculate the diff between the renderableVDOM and the previous VDOM here
const diff = getRenderableVDOMDiff(renderableVDOM, vdom);
vdom.previous = vdom.current;
vdom.current = [];
updateCallback(renderableVDOM, diff);
afterUpdate();
};
const hooks = createHooks(update, registerOnUpdatedCallback);
update(hooks);
};
export const { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } = React;