-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflux.go
45 lines (39 loc) · 946 Bytes
/
flux.go
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
package main
import "time"
const renderInterval = 50 * time.Millisecond
// Store for Flux
type Store struct {
Actions chan Action
}
// NewStore is the Store constructor
func NewStore() Store {
store := Store{}
store.Actions = make(chan Action, 100)
return store
}
// ReduceLoop will continually apply actions to state
func (store Store) ReduceLoop(state AppState) {
// This debouncing logic allows us to keep applying state changes, but only render every renderInterval
debouncer := time.After(renderInterval)
rendered := true
for {
if rendered {
action := <-store.Actions
state = action.Apply(state)
rendered = false
}
select {
case <-debouncer:
Render(state)
debouncer = time.After(renderInterval)
rendered = true
case action := <-store.Actions:
state = action.Apply(state)
rendered = false
}
}
}
// Action represents a change to take place
type Action interface {
Apply(AppState) AppState
}