-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
37 lines (34 loc) · 793 Bytes
/
core.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
export default function html([first, ...strings], ...values) {
return values
.reduce((acc, cur) => acc.concat(cur, strings.shift()), [first])
.filter((x) => (x && x !== true) || x === 0)
.join("");
}
export function createStore(reducer) {
let state = reducer();
const roots = new Map();
function render() {
for (const [root, component] of roots) {
const output = component();
root.innerHTML = output;
}
}
return {
attach(component, root) {
roots.set(root, component);
render();
},
connect(selector = (state) => state) {
return (component) =>
(props, ...args) =>
component(
Object.assign({}, props, selector(state)),
...args
);
},
dispatch(action, ...args) {
state = reducer(state, action, args);
render();
},
};
}