Last active
January 10, 2017 09:02
-
-
Save Qata/fd2ef4e287e9bea40992ff6c39dd7d09 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 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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An example usage instead of using
subscribe
andunsubscribe
would be to just callstore.producer.subscribeNext
any time that we need data, that way we don't need to be in a delegated model specifically.