Skip to content

Instantly share code, notes, and snippets.

@larryonoff
Created July 24, 2019 06:10
Show Gist options
  • Save larryonoff/943fdc69c6563095f7debec3b45635ea to your computer and use it in GitHub Desktop.
Save larryonoff/943fdc69c6563095f7debec3b45635ea to your computer and use it in GitHub Desktop.
Re
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