Skip to content

Instantly share code, notes, and snippets.

@bradphilips
Created August 4, 2017 00:20
Show Gist options
  • Save bradphilips/0b6fbbbddb276df541e23beea66860bb to your computer and use it in GitHub Desktop.
Save bradphilips/0b6fbbbddb276df541e23beea66860bb to your computer and use it in GitHub Desktop.
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