-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslices.js
More file actions
101 lines (91 loc) · 2.1 KB
/
slices.js
File metadata and controls
101 lines (91 loc) · 2.1 KB
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
import { h, app } from "hyperapp"
import "./index.css"
const createLogger = () => {
return {
state: {
logs: []
},
actions: {
log: message => state => ({
logs: state.logs.concat("[" + new Date().toUTCString() + "] " + message)
})
}
}
}
const createSlice1 = () => {
let logger
return {
state: {},
actions: {
init: dependencies => {
logger = dependencies.logger
},
action1: () => {
logger.log("slice1.action1()")
},
action2: () => {
logger.log("slice1.action2()")
}
}
}
}
const createSlice2 = () => {
let logger, slice1
return {
state: {},
actions: {
init: dependencies => {
logger = dependencies.logger
slice1 = dependencies.slice1
},
action1: () => {
slice1.action1()
logger.log("slice2.action1()")
},
action2: () => {
slice1.action2()
logger.log("slice2.action2()")
}
}
}
}
const logger = createLogger()
const slice1 = createSlice1()
const slice2 = createSlice2()
const state = {
logger: logger.state,
slice1: slice1.state,
slice2: slice2.state
}
const actions = {
logger: logger.actions,
slice1: slice1.actions,
slice2: slice2.actions,
// inject all the dependencies between modules/slices
init: () => (_, actions) => {
actions.slice1.init(actions)
actions.slice2.init(actions)
}
}
const view = (state, actions) => (
<main>
<h1>Dependencies between slices</h1>
<div class="content">
<h2>Slice 1</h2>
<div class="buttons">
<button onclick={actions.slice1.action1}>Action 1</button>
<button onclick={actions.slice1.action2}>Action 2</button>
</div>
<h2>Slice 2</h2>
<div class="buttons">
<button onclick={actions.slice2.action1}>Action 1</button>
<button onclick={actions.slice2.action2}>Action 2</button>
</div>
<h2>Log</h2>
<div class="log">
{state.logger.logs.map(message => <span>{message}</span>)}
</div>
</div>
</main>
)
app(state, actions, view, document.body).init()