-
-
Save rpassis/1800c247234d6a6f25335f60f0d134ca to your computer and use it in GitHub Desktop.
ReSwift + Rx
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 | |
public class RxStore: StoreSubscriber { | |
private var stateObservable:Observable<AppState> { | |
return subject.asObserver() | |
.shareReplay(1) | |
} | |
private var subject = PublishSubject<AppState>() | |
private var store: Store<AppState> | |
init(store: Store<AppState>) { | |
self.store = store | |
self.store.subscribe(self) | |
} | |
deinit { | |
self.store.unsubscribe(self) | |
} | |
public func newState(newState: AppState) { | |
subject.onNext(newState) | |
} | |
public func state<S:Equatable>(mapState:(AppState)->S) -> Observable<S> | |
{ | |
return stateObservable | |
.map(mapState) | |
.distinctUntilChanged() | |
} | |
} | |
var store = Store<AppState>(reducer: AppReducer(), state: nil, middleware: [rxMiddleware, loggingMiddleware]) | |
var rxStore = RxStore(store: store) | |
rxStore.state { $0.userState } | |
.subscribeNext { | |
print("user: \($0.user)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment