Skip to content

Commit 8ad2062

Browse files
Merge pull request #23 from StevenLambion/bugs/fix-ui-updates
Fix Stale UI When Using ActionDispatcher In Some Cases
2 parents 83be78d + ed85493 commit 8ad2062

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

Sources/SwiftDux/UI/MappedState.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public struct MappedState<State>: DynamicProperty {
1616

1717
@EnvironmentObject private var connection: StateConnection<State>
1818

19+
// Needed by SwiftUI in case StateBinder is used. This attaches the required
20+
// subscriptions.
21+
@Environment(\.actionDispatcher) private var actionDispatcher: ActionDispatcher
22+
1923
public var wrappedValue: State {
2024
connection.latestState!
2125
}

Sources/SwiftDux/UI/StateConnection.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ import Foundation
66
/// It uses a "change publisher" to notify it to retrieve a new version of the state object. This publisher
77
/// typically fires from the store after the state has been modified, or directly from an action being dispatched.
88
internal final class StateConnection<State>: ObservableObject, Identifiable {
9-
109
@Published var latestState: State?
1110

1211
var getState: () -> State?
1312

1413
private var cancellable: Cancellable? = nil
1514

16-
init(getState: @escaping () -> State?, changePublisher: AnyPublisher<Void, Never>) {
15+
init(getState: @escaping () -> State?, changePublisher: AnyPublisher<Void, Never>? = nil) {
1716
self.getState = getState
1817
self.latestState = getState()
19-
self.cancellable = changePublisher.sink { [weak self] in
18+
self.cancellable = changePublisher?.sink { [weak self] in
2019
guard let self = self else { return }
2120
self.latestState = getState()
2221
}

Sources/SwiftDux/UI/ViewModifiers/StateConnectionViewModifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal struct StateConnectionViewModifier<Superstate, State>: ViewModifier {
4141
},
4242
changePublisher: hasUpdate
4343
? storeUpdated.filter(filter).map { _ in }.eraseToAnyPublisher()
44-
: dispatchConnection.didDispatchAction.eraseToAnyPublisher()
44+
: nil
4545
)
4646
return stateConnection
4747
}

0 commit comments

Comments
 (0)