@@ -10,49 +10,53 @@ import Foundation
10
10
import RxSwift
11
11
import RxCocoa
12
12
13
- /// A Reducer mutates an input state into an output state according to an action
14
- public typealias Reducer < StateType: Equatable > = ( _ state: StateType , _ action: Action ) -> StateType
15
-
16
- /// A Middleware has not effect on the state, it us just triggered by a dispatch action
17
- public typealias Middleware < StateType: Equatable > = ( _ state: StateType , _ action: Action ) -> Void
13
+ ///// A Reducer mutates an input state into an output state according to an action
14
+ // public typealias Reducer<StateType: Equatable> = (_ state: StateType, _ action: Action) -> StateType
15
+ //
16
+ ///// A Middleware has not effect on the state, it us just triggered by a dispatch action
17
+ // public typealias Middleware<StateType: Equatable> = (_ state: StateType, _ action: Action) -> Void
18
18
19
19
/// A Store holds the state, mutate the state through actions / reducers and exposes the state via a Driver
20
20
/// A Store is dedicated to a State Type
21
- public final class Store < StateType : Equatable > {
21
+ public final class Store < State : Equatable > {
22
22
23
- private var state : StateType
24
- private let reducers : ContiguousArray < Reducer < StateType > >
25
- private let middlewares : ContiguousArray < Middleware < StateType > > ?
23
+ public typealias ReducerFunction = ( State , Action ) -> State
26
24
27
- public init ( withState state: StateType ,
28
- withReducers reducers: ContiguousArray < Reducer < StateType > > ,
29
- withMiddlewares middlewares: ContiguousArray < Middleware < StateType > > ? = nil ) {
25
+ private var state : State
26
+ private var reducers = [ ReducerFunction] ( )
27
+ // private let reducers: ContiguousArray<Reducer<StateType>>
28
+ // private let middlewares: ContiguousArray<Middleware<StateType>>?
29
+
30
+ public init ( withState state: State ) {
30
31
self . state = state
31
- self . reducers = reducers
32
- self . middlewares = middlewares
32
+ // self.middlewares = middlewares
33
+ }
34
+
35
+ public func register< SubState: Equatable > ( reducer: Reducer < State , SubState > ) {
36
+ self . reducers. append ( reducer. apply)
33
37
}
34
38
35
- public func dispatch( action: Action ) -> Observable < StateType > {
39
+ public func dispatch( action: Action ) -> Observable < State > {
36
40
// every received action is converted to an async action
37
41
return action
38
42
. toAsync ( )
39
- . do ( onNext: { [ unowned self] ( action) in
40
- self . middlewares? . forEach ( { [ unowned self] ( middleware) in
41
- middleware ( self . state, action)
42
- } )
43
- } )
44
- . map { ( action) -> StateType in
45
-
46
- return self . reducers. reduce ( self . state, { ( previousState , reducer) -> StateType in
47
- return reducer ( previousState , action)
43
+ // .do(onNext: { [unowned self] (action) in
44
+ // self.middlewares?.forEach({ [unowned self] (middleware) in
45
+ // middleware(self.state, action)
46
+ // })
47
+ // })
48
+ . map { ( action) -> State in
49
+
50
+ return self . reducers. reduce ( self . state, { ( currentState , reducer) -> State in
51
+ return reducer ( currentState , action)
48
52
} )
49
53
} . do ( onNext: { [ unowned self] ( newState) in
50
54
self . state = newState
51
55
} ) . distinctUntilChanged ( )
52
56
}
53
57
54
- public func dispatch< SubStateType: Equatable > ( action: Action , on: @escaping ( StateType ) -> SubStateType ) -> Observable < SubStateType > {
55
- return self . dispatch ( action: action) . map { on ( $0) }
56
- }
58
+ // public func dispatch<SubStateType: Equatable>(action: Action, on: @escaping (State ) -> SubStateType) -> Observable<SubStateType> {
59
+ // return self.dispatch(action: action).map { on($0) }
60
+ // }
57
61
58
62
}
0 commit comments