-
Notifications
You must be signed in to change notification settings - Fork 0
/
wyspa.js
executable file
·52 lines (45 loc) · 1.33 KB
/
wyspa.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
const t = document.createElement("template");
export default
function key(strings, ...values) {
return strings.flatMap(str => [str].concat(values.shift()));
}
export
function render(component, reducer) {
let state;
let prev = [];
function withState(part) {
if (typeof part === "function") {
return part(state).map(withState);
}
return part;
}
function paint(island) {
t.innerHTML = island.flat(Infinity).join("");
document.getElementById(t.content.children[0].id).replaceWith(t.content);
}
function* diff(prev, curr) {
let queue = [];
let length = Math.max(prev.length, curr.length);
for (let i = 0; i < length; i++) {
if (prev[i] instanceof Array && curr[i] instanceof Array) {
queue.push([prev[i], curr[i]]);
} else if (prev[i] !== curr[i]) {
yield curr;
}
}
for (let pair of queue) {
yield * diff(...pair);
}
}
function dispatch(action, ...args) {
state = reducer(state, action, args);
let tree = component(state).map(withState);
for (let part of diff(prev, tree)) {
paint(part);
}
prev = tree;
return cleanUp => cleanUp(state);
}
dispatch();
return dispatch;
}