Introduce StateStore with bindings example for Chat example.

This commit is contained in:
David Langley
2021-09-15 11:47:59 +01:00
parent f3dbd2a056
commit 7aad7c51ba
8 changed files with 37 additions and 39 deletions
@@ -40,21 +40,25 @@ extension BindableState where BindStateType == Void {
class ViewModelContext<ViewState:BindableState, ViewAction>: ObservableObject {
private var cancellables = Set<AnyCancellable>()
fileprivate let viewActions: PassthroughSubject<ViewAction, Never>
let inputActions: PassthroughSubject<ViewAction, Never>
@Published var inputState: ViewState.BindStateType
@Published var bindings: ViewState.BindStateType
@Published fileprivate(set) var viewState: ViewState
init(initialViewState: ViewState) {
self.inputState = initialViewState.bindings
self.inputActions = PassthroughSubject()
self.bindings = initialViewState.bindings
self.viewActions = PassthroughSubject()
self.viewState = initialViewState
if !(initialViewState.bindings is Void) {
self.$inputState
self.$bindings
.weakAssign(to: \.viewState.bindings, on: self)
.store(in: &cancellables)
}
}
func send(viewAction: ViewAction) {
viewActions.send(viewAction)
}
}
@available(iOS 14, *)
@@ -73,17 +77,18 @@ class StateStoreViewModel<State:BindableState, StateAction, ViewAction> {
self.state = CurrentValueSubject(initialViewState)
self.state.weakAssign(to: \.context.viewState, on: self)
.store(in: &cancellables)
self.context.inputActions.sink { [weak self] action in
self.context.viewActions.sink { [weak self] action in
guard let self = self else { return }
self.process(viewAction: action)
}
.store(in: &cancellables)
}
func dispatch(action: StateAction) {
reducer(state: &state.value, action: action)
Self.reducer(state: &state.value, action: action)
}
func reducer(state: inout State, action: StateAction) {
class func reducer(state: inout State, action: StateAction) {
}