diff --git a/Sources/RecombinePackage/Store.swift b/Sources/RecombinePackage/Store.swift index a684b68..a86cd15 100644 --- a/Sources/RecombinePackage/Store.swift +++ b/Sources/RecombinePackage/Store.swift @@ -34,7 +34,6 @@ public class Store: ObservableObject { .store(in: &cancellables) } - @available(iOS 14, *) public func lensing(_ keyPath: KeyPath) -> StoreTransform { .init(store: self, lensing: keyPath) } @@ -67,16 +66,20 @@ public class StoreTransform: ObservableObject { public private(set) var state: State private let store: Store private let keyPath: KeyPath + private var cancellables = Set() - @available(iOS 14, *) public required init(store: Store, lensing keyPath: KeyPath) { self.store = store self.keyPath = keyPath state = store.state[keyPath: keyPath] - store.$state.map { $0[keyPath: keyPath] }.assign(to: &$state) + store.$state + .map { $0[keyPath: keyPath] } + .sink { [unowned self] state in + self.state = state + } + .store(in: &cancellables) } - @available(iOS 14, *) public func lensing(_ keyPath: KeyPath) -> StoreTransform { .init(store: store, lensing: self.keyPath.appending(path: keyPath)) } @@ -89,3 +92,17 @@ public class StoreTransform: ObservableObject { store.dispatch(actions) } } + +extension StoreTransform: Subscriber { + public func receive(subscription: Subscription) { + subscription.store(in: &cancellables) + subscription.request(.unlimited) + } + + public func receive(_ input: Action) -> Subscribers.Demand { + store.dispatch(input) + return .unlimited + } + + public func receive(completion: Subscribers.Completion) {} +}