-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (36 loc) · 950 Bytes
/
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
import flyd from "flyd";
import merge from "mergerino";
// merge : (state, val) => state
window.merge = merge;
// State
const stateStream = flyd.stream();
const initialState = {
user: {
name: "John",
weight: 180,
age: 34,
height: 177,
},
other: {
many: true,
properties: true,
},
};
const states = flyd.scan(merge, initialState, stateStream);
// Actions
const addAction = (state) => ({ ...state, user: { age: state.user.age + 1 } });
const subtractAction = (state) => ({
...state,
user: { age: state.user.age - 1 },
});
// View
const addButton = document.getElementById("add");
const subtractButton = document.getElementById("subtract");
const viewState = document.getElementById("state");
addButton.onclick = () => stateStream(addAction);
subtractButton.onclick = () => stateStream(subtractAction);
states.map((state) => {
viewState.innerText = state.user.age;
console.log(state);
return state;
});