-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.go
More file actions
49 lines (39 loc) · 1.08 KB
/
middleware.go
File metadata and controls
49 lines (39 loc) · 1.08 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
package godux
type MiddlewareContext struct {
State func() State
Dispatch Dispatcher
}
type Middleware func(*MiddlewareContext) func(Dispatcher) Dispatcher
func Apply(middlewares ...Middleware) func(StoreFactory) StoreFactory {
return func(createStore StoreFactory) StoreFactory {
return func(si *StoreInput) *Store {
store := createStore(si)
dispatch := store.dispatcher
chain := make([]interface{}, 0, 0)
context := &MiddlewareContext{
State: store.State,
Dispatch: func(a *Action) *Action {
return dispatch(a)
},
}
for _, middleware := range middlewares {
chain = append(chain, middleware(context))
}
dispatch = Compose(chain...)(dispatch).(Dispatcher)
store.dispatcher = dispatch
return store
}
}
}
func CreateSimpleMiddleware(callback func(*MiddlewareContext, Dispatcher, *Action) *Action) Middleware {
return func(c *MiddlewareContext) func(Dispatcher) Dispatcher {
return func(d Dispatcher) Dispatcher {
if d == nil {
d = c.Dispatch
}
return func(a *Action) *Action {
return callback(c, d, a)
}
}
}
}