Created
July 24, 2019 06:10
-
-
Save larryonoff/943fdc69c6563095f7debec3b45635ea to your computer and use it in GitHub Desktop.
Re
This file contains 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 ReactiveSwift | |
public protocol ActionProtocol {} | |
public protocol ViewStateProtocol {} | |
public typealias Reducer<ViewState: ViewStateProtocol> = SignalProducer<ViewState, Never> | |
open class ReducerFactory<Action: ActionProtocol, ViewState: ViewStateProtocol> { | |
public init() {} | |
open func reduce( | |
_ action: Action, | |
viewState: ViewState | |
) -> Reducer<ViewState> { | |
return .empty | |
} | |
} | |
open class Store<Action: ActionProtocol, ViewState: ViewStateProtocol> { | |
public var viewState: ViewState { | |
return mutableViewState.value | |
} | |
private let mutableViewState: MutableProperty<ViewState> | |
private let reducerFactory: ReducerFactory<Action, ViewState> | |
private let scheduler: QueueScheduler | |
public init( | |
initialViewState: ViewState, | |
reducerFactory: ReducerFactory<Action, ViewState> | |
) { | |
self.mutableViewState = MutableProperty(initialViewState) | |
self.reducerFactory = reducerFactory | |
self.scheduler = QueueScheduler(qos: .userInitiated) | |
} | |
public func subscribe( | |
_ update: @escaping (ViewState) -> Void | |
) -> Disposable? { | |
return mutableViewState.producer | |
.observe(on: UIScheduler()) | |
.startWithValues { update($0) } | |
} | |
open func dispatch(_ action: Action) { | |
mutableViewState <~ reducerFactory | |
.reduce(action, viewState: mutableViewState.value) | |
.start(on: scheduler) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment