Created
August 4, 2017 00:20
-
-
Save bradphilips/0b6fbbbddb276df541e23beea66860bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import RxSwift | |
import ReSwift | |
class RxStore<State>: Store<AppState>, StoreSubscriber where State : StateType { | |
public typealias StoreSubscriberStateType = State | |
private var stateObservable:Observable<State> { | |
return subject.asObserver() | |
.shareReplay(1) | |
} | |
private var subject = PublishSubject<State>() | |
required init(reducer: @escaping (Action, State?) -> State, state: State?, middleware: [Middleware<State>] = []) { | |
super.init(reducer: reducer, state: state, middleware: middleware) | |
self.subscribe(self) | |
} | |
deinit { | |
self.unsubscribe(self) | |
} | |
func newState(state newState: State) { | |
subject.onNext(newState) | |
} | |
func observe<S>(mapState:@escaping (State) -> S) -> Observable<S> { | |
return stateObservable | |
.map(mapState) | |
} | |
} | |
let mainStore = RxStore<AppState>( | |
reducer: applicationReducer, | |
state: nil | |
) | |
mainStore | |
.observe { $0.navigationState } | |
.subscribe(onNext: { navigationState in | |
print("Route: \(navigationState.route)") | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment