-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrank.js
164 lines (139 loc) · 3.63 KB
/
crank.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
function wrap(value) {
return value === undefined ? [] : Array.isArray(value) ? value : [value];
}
function unwrap(arr) {
return arr.length <= 1 ? arr[0] : arr;
}
class Element {
constructor(tag, props) {
this.tag = tag;
this.props = props;
this._node = undefined;
this._children = undefined;
// flags
this._isMounted = false;
}
}
export const Portal = Symbol.for("crank.Portal");
export function createElement(tag, props, ...children) {
props = Object.assign({}, props);
if (children.length === 1) {
props.children = children[0];
} else if (children.length > 1) {
props.children = children;
}
return new Element(tag, props);
}
function narrow(value) {
if (typeof value === "boolean" || value == null) {
return undefined;
} else if (typeof value === "string" || value instanceof Element) {
return value;
}
return value.toString();
}
export class Renderer {
constructor() {
this._cache = new WeakMap();
}
render(children, root) {
let portal = this._cache.get(root);
if (portal) {
portal.props = {root, children};
} else {
portal = createElement(Portal, {root, children});
this._cache.set(root, portal);
}
return update(this, portal);
}
create(el) {
return document.createElement(el.tag);
}
patch(el, node) {
for (let [name, value] of Object.entries(el.props)) {
if (name === "children") {
continue;
} else if (name === "class") {
name = "className";
}
if (name in node) {
node[name] = value;
} else {
node.setAttribute(name, value);
}
}
}
arrange(el, node, children) {
let child = node.firstChild;
for (const newChild of children) {
if (child === newChild) {
child = child.nextSibling;
} else if (typeof newChild === "string") {
if (child !== null && child.nodeType === Node.TEXT_NODE) {
child.nodeValue = newChild;
child = child.nextSibling;
} else {
node.insertBefore(document.createTextNode(newChild), child);
}
} else {
node.insertBefore(newChild, child);
}
}
while (child !== null) {
const nextSibling = child.nextSibling;
node.removeChild(child);
child = child.nextSibling;
}
}
}
function diff(renderer, oldChild, newChild) {
if (
oldChild instanceof Element &&
newChild instanceof Element &&
oldChild.tag === newChild.tag
) {
if (oldChild !== newChild) {
oldChild.props = newChild.props;
newChild = oldChild;
}
}
let value;
if (newChild instanceof Element) {
value = update(renderer, newChild);
} else {
value = newChild;
}
return [newChild, value];
}
function update(renderer, el) {
if (el._isMounted) {
el = createElement(el, {...el.props});
}
const oldChildren = wrap(el._children);
const newChildren = wrap(el.props.children);
const children = [];
const values = [];
const length = Math.max(oldChildren.length, newChildren.length);
for (let i = 0; i < length; i++) {
const oldChild = oldChildren[i];
const newChild = narrow(newChildren[i]);
const [child, value] = diff(renderer, oldChild, newChild);
children.push(child);
if (value) {
values.push(value);
}
}
el._children = unwrap(children);
return commit(renderer, el, values);
}
function commit(renderer, el, values) {
if (el.tag === Portal) {
renderer.arrange(el, el.props.root, values);
return undefined;
} else if (!el._node) {
el._node = renderer.create(el);
}
renderer.patch(el, el._node);
renderer.arrange(el, el._node, values);
return el._node;
}