Add StateStoreViewModel and publisher extensions for convenienec.

This commit is contained in:
David Langley
2021-09-14 22:28:25 +01:00
parent 3d65fbd48f
commit da5fcd5d4f
8 changed files with 159 additions and 31 deletions
@@ -0,0 +1,26 @@
import Foundation
import Combine
@available(iOS 14.0, *)
extension Publisher {
func sinkDispatchTo<S, SA, IA>(_ store: StateStoreViewModel<S, SA, IA>) where SA == Output, Failure == Never {
return self
.subscribe(on: DispatchQueue.main)
.sink { [weak store] (output) in
guard let store = store else { return }
store.dispatch(action: output)
}
.store(in: &store.cancellables)
}
func dispatchTo<S, SA, IA>(_ store: StateStoreViewModel<S, SA, IA>) -> Publishers.HandleEvents<Publishers.SubscribeOn<Self, DispatchQueue>> where SA == Output, Failure == Never {
return self
.subscribe(on: DispatchQueue.main)
.handleEvents(receiveOutput: { [weak store] action in
guard let store = store else { return }
store.dispatch(action: action)
})
}
}