-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantireact.js
236 lines (192 loc) · 6.3 KB
/
antireact.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
const AntiReact = (function () {
/*******************************************************************************/
/* EVENTS */
/*******************************************************************************/
/**
* The render event manager
*/
const renderPubSub = (() => {
const _event = new Event('render');
const _listener = new EventTarget();
return ({
listen: f => _listener.addEventListener('render', f),
emit: () => _listener.dispatchEvent(_event)
});
})();
/*******************************************************************************/
/* UTILS */
/*******************************************************************************/
/**
* Just converting an object into a two-dimensional array
*
* @param {object} obj object to convert
*/
const pair = obj => Object.keys(obj).map(k => [k, obj[k]]);
/**
* Recursively applies arbitrarily nested properties to HTMLElement
*
* @param {HTMLElement} $el element to change properties on
* @param {object} props key/value pair of properties, can be nested
*/
const _props = ($el, props) =>
pair(props).forEach(([k, v]) => {
if(typeof v === "object") {
_props($el[k], v);
} else if(typeof v === "boolean") {
$el.setAttribute(k, v);
$el[k] = v;
} else {
$el[k] = v;
}
});
/*******************************************************************************/
/* DOM */
/*******************************************************************************/
/**
* Create Virtual DOM elements
*
* @param {string|Component} item Component or HTML tag
* @param {object} props properties of element
* @param {array} children children of element
*/
const dom = (item, props, ...children) =>
({ item, props: props || {}, children })
const createElement = (item, props) => {
const el = document.createElement(item);
_props(el, props);
return el;
}
/**
* Converts a component to a Virtual DOM tree by evaluating render function
*
* @param {Component} component Component to convert
* @param {object} props properties
*/
const componentToTree = (component, props) =>
component.render ?
component.render(props) :
component(props);
/**
* Takes an unexpanded Virtual DOM tree (as in components are still unevaulated) and
* expands it onto a fully evaluated structure
*
* @param {object} tree Virtual DOM tree
*/
const expandTree = tree => {
if(!tree || !tree.item) return tree;
const el = typeof tree.item === "string" ?
tree :
componentToTree(tree.item, tree.props);
if(Array.isArray(el.children)) {
el.children = el.children.map(expandTree);
}
return el;
}
/**
* Converts a Virtual DOM node into HTML Elements
*
* @param {VirtualDOMNode} node node to be converted
*/
const nodeToHTML = node => {
if(typeof node === "string")
return document.createTextNode(node);
const $el = createElement(node.item, node.props);
node.children
.map(nodeToHTML)
.forEach(child => $el.appendChild(child));
return $el;
}
/*******************************************************************************/
/* DIFFING */
/*******************************************************************************/
/**
* Check if two nodes are different
*/
const nodeChanged = (a, b) =>
typeof a !== typeof b ||
typeof a === "string" && a !== b ||
a.item !== b.item;
/**
* Updates the DOM by diffing
*
* @param {HTMLElement} $parent Parent DOM Node
* @param {VirtualDOMNode} newNode new Virtual DOM Node to insert
* @param {VirtualDOMNode} oldNode old Virtual DOM Node to be replaced
* @param {int} index current index
*/
const updateElement = ($parent, newNode, oldNode, index = 0) => {
if(!oldNode) {
$parent.appendChild(nodeToHTML(newNode));
} else if (!newNode) {
$parent.removeChild(
$parent.childNodes[index]
);
} else if (nodeChanged(newNode, oldNode)) {
$parent.replaceChild(
nodeToHTML(newNode),
$parent.childNodes[index]
);
} else if (newNode.item) {
const newlen = newNode.children.length;
const oldlen = oldNode.children.length;
for(let i = 0; i < newlen || i < oldlen; i++) {
updateElement(
$parent.childNodes[index],
newNode.children[i],
oldNode.children[i],
i
);
}
}
}
/*******************************************************************************/
/* COMPONENTS */
/*******************************************************************************/
/**
* Creates a stateful component
*
* @param {object} component Stateless shell that gets wrapped with statefulness
*/
const statefulComponent = component => {
let state = component.initialState;
const setState = (newState) => {
if(typeof component.state === "object") {
Object.assign({}, component.state, newState);
} else {
state = newState;
}
renderPubSub.emit();
}
const dispatch = f => {
setState(f(state));
}
return ({
render(props) {
return component.render(state, dispatch, props)
}
});
};
/*******************************************************************************/
/* INITIALIZATION */
/*******************************************************************************/
/**
* Create your app!
*
* @param {VirtualDOMElement} vdom Virtual DOM Element
* @param {HTMLElement} $parent Parent DOM node
*/
const createApp = (vdom, $parent) => {
let currentTree;
$parent.innerHTML = '';
renderPubSub.listen(() => {
updateElement($parent, expandTree(vdom), currentTree);
currentTree = expandTree(vdom);
});
renderPubSub.emit();
}
return {
dom,
createApp,
statefulComponent,
};
})();