Skip to content

Commit

Permalink
Change StoreBinding to a DynamicProperty to allow for view invalidati…
Browse files Browse the repository at this point in the history
…on when updated through other sources
  • Loading branch information
Qata committed Jul 5, 2021
1 parent e4ba428 commit affb703
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Sources/RecombinePackage/Reducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public extension Reducer {
}

init<S: Sequence>(_ reducers: S) where S.Element: Reducer, S.Element.Transform == Transform {
self = reducers.reduce(Self()) {
self = reducers.reduce(.init()) {
$0.concat($1)
}
}
Expand Down
43 changes: 35 additions & 8 deletions Sources/RecombinePackage/StoreBinding.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import Combine
import SwiftUI

@available(macOS 11.0, iOS 14, watchOS 7, tvOS 14, *)
@propertyWrapper
public struct StoreBinding<Value, Store: StoreProtocol> {
private let store: Store

private let stateLens: (Store.SubState) -> Value
public struct StoreBinding<Value: Equatable, Store: StoreProtocol>: DynamicProperty {
@StateObject private var store: LensedStore<
Store.BaseState,
Value,
Store.RawAction,
Store.BaseRefinedAction,
Store.SubRefinedAction
>
private let actionTransform: (Value) -> ActionStrata<Store.RawAction, Store.SubRefinedAction>

public var wrappedValue: Value { stateLens(store.state) }

private init(
store: Store,
stateLens: @escaping (Store.SubState) -> Value,
action: @escaping (Value) -> ActionStrata<Store.RawAction, Store.SubRefinedAction>
) {
self.store = store
self.stateLens = stateLens
actionTransform = action
_store = .init(wrappedValue: store.lensing(state: stateLens))
}

public init(
Expand All @@ -27,6 +30,13 @@ public struct StoreBinding<Value, Store: StoreProtocol> {
self.init(store: store, stateLens: stateLens, action: { .raw(rawAction($0)) })
}

public init(
_ store: Store,
rawAction: @escaping (Value) -> Store.RawAction
) where Store.SubState == Value {
self.init(store: store, stateLens: { $0 }, action: { .raw(rawAction($0)) })
}

public init(
_ store: Store,
state stateLens: @escaping (Store.SubState) -> Value,
Expand All @@ -35,6 +45,19 @@ public struct StoreBinding<Value, Store: StoreProtocol> {
self.init(store: store, stateLens: stateLens, action: { .refined(refinedAction($0)) })
}

public init(
_ store: Store,
refinedAction: @escaping (Value) -> Store.SubRefinedAction
) where Store.SubState == Value {
self.init(store: store, stateLens: { $0 }, action: { .refined(refinedAction($0)) })
}

public init(
_ store: Store
) where Store.SubState == Value, Store.SubRefinedAction == Value {
self.init(store: store, stateLens: { $0 }, action: { .refined($0) })
}

public var projectedValue: Binding<Value> {
Binding<Value>(
get: { wrappedValue },
Expand All @@ -48,4 +71,8 @@ public struct StoreBinding<Value, Store: StoreProtocol> {
}
)
}

public var wrappedValue: Value {
store.state
}
}

0 comments on commit affb703

Please sign in to comment.