Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public typealias Reducer<Value, Action, Effect> = (inout Value, Action) -> Command<Effect>
- public final class Store<Value, Action>: ObservableObject {
- //private let reducer: (inout Value, Action) -> Void
- private var _send: ((Action) -> Void)?
- @Published public private(set) var value: Value
- private var cancellable: Cancellable?
- // the reducer for the main Store needs to be generic on <Value, Action, Action>,
- // but other reducers are generic on <Value, Action, Effect>
- public init(initialValue: Value, reducer: @escaping Reducer<Value, Action, Action>) {
- self.value = initialValue
- self._send = { action in
- let command = reducer(&self.value, action)
- // TODO do something with the command in the runtime...
- }
- }
- private init(initialValue: Value, send: @escaping (Action) -> Void) {
- self.value = initialValue
- self._send = send
- }
- public func send(_ action: Action) {
- self._send?(action)
- }
- public func view<LocalValue, LocalAction>(
- value toLocalValue: @escaping (Value) -> LocalValue,
- action toGlobalAction: @escaping (LocalAction) -> Action
- ) -> Store<LocalValue, LocalAction> {
- let localStore = Store<LocalValue, LocalAction>(
- initialValue: toLocalValue(self.value),
- send: { action in
- self.send(toGlobalAction(action))
- })
- localStore.cancellable = self.$value.sink { [weak localStore] newValue in
- localStore?.value = toLocalValue(newValue)
- }
- return localStore
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement