Skip to content

Instantly share code, notes, and snippets.

@Qata
Last active January 10, 2017 09:02
Show Gist options
  • Save Qata/fd2ef4e287e9bea40992ff6c39dd7d09 to your computer and use it in GitHub Desktop.
Save Qata/fd2ef4e287e9bea40992ff6c39dd7d09 to your computer and use it in GitHub Desktop.
import ReactiveCocoa
import ReSwift
import Result
class ObservableStore<State: StateType>: Store<State> {
internal let observable: MutableProperty<State>
var producer: SignalProducer<State, NoError> {
get {
return observable.producer
}
}
var signal: Signal<State, NoError> {
get {
return observable.signal
}
}
override var state: State! {
didSet {
observable.value = state
}
}
convenience init(reducer: AnyReducer, state: State) {
self.init(reducer: reducer, state: state, middleware: [])
}
required init(reducer: AnyReducer, state: State, middleware: [Middleware]) {
observable = MutableProperty<State>(state)
super.init(reducer: reducer, state: state, middleware: middleware)
}
func dispatch(producer: SignalProducer<ReSwift.Action, NoError>) {
producer.startWithNext { [unowned self] in
self.dispatch($0)
}
}
func dispatch(signal: Signal<ReSwift.Action, NoError>) {
signal.observeNext { [unowned self] in
self.dispatch($0)
}
}
}
@Qata
Copy link
Author

Qata commented Jan 10, 2017

An example usage instead of using subscribe and unsubscribe would be to just call store.producer.subscribeNext any time that we need data, that way we don't need to be in a delegated model specifically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment