-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReducer.swift
39 lines (32 loc) · 1.13 KB
/
Reducer.swift
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
public struct Reducer<State, SyncAction, Environment> {
public typealias Transform = (_ state: inout State, _ action: SyncAction, _ environment: Environment) -> Void
public let transform: Transform
public init() {
transform = { _, _, _ in }
}
public init(transform: @escaping Transform) {
self.transform = transform
}
public init(_ reducers: Self...) {
self = .init(reducers)
}
public init<S: Sequence>(_ reducers: S) where S.Element == Self {
self = reducers.reduce(.init()) {
$0.concat($1)
}
}
public func callAsFunction(state: inout State, action: SyncAction, environment: Environment) {
transform(&state, action, environment)
}
public func concat(_ other: Reducer) -> Self {
Self.init { state, action, environment in
self.transform(&state, action, environment)
other.transform(&state, action, environment)
}
}
public func reduce(state: State, action: SyncAction, environment: Environment) -> State {
var s = state
transform(&s, action, environment)
return s
}
}