Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address Reducer._printChanges() sendability #3320

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions Sources/ComposableArchitecture/Reducer/Reducers/DebugReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ extension Reducer {

private let printQueue = DispatchQueue(label: "co.pointfree.swift-composable-architecture.printer")

public struct _ReducerPrinter<State, Action> {
private let _printChange: (_ receivedAction: Action, _ oldState: State, _ newState: State) -> Void
public struct _ReducerPrinter<State, Action>: Sendable {
private let _printChange: @Sendable (
_ receivedAction: Action,
_ oldState: State,
_ newState: State
) -> Void
@usableFromInline
let queue: DispatchQueue

public init(
printChange: @escaping (_ receivedAction: Action, _ oldState: State, _ newState: State) -> Void,
printChange: @escaping @Sendable (
_ receivedAction: Action,
_ oldState: State,
_ newState: State
) -> Void,
queue: DispatchQueue? = nil
) {
self._printChange = printChange
Expand Down Expand Up @@ -77,17 +85,23 @@ public struct _PrintChangesReducer<Base: Reducer>: Reducer {
) -> Effect<Base.Action> {
if let printer = self.printer {
return withSharedChangeTracking { changeTracker in
let oldState = state
let oldState = UncheckedSendable(state)
let effects = self.base.reduce(into: &state, action: action)
return withEscapedDependencies { continuation in
effects.merge(
with: .publisher { [newState = state, queue = printer.queue] in
with: .publisher { [
newState = UncheckedSendable(state),
action = UncheckedSendable(action),
queue = printer.queue
] in
Deferred<Empty<Action, Never>> {
queue.async {
continuation.yield {
changeTracker.assert {
printer.printChange(
receivedAction: action, oldState: oldState, newState: newState
receivedAction: action.wrappedValue,
oldState: oldState.wrappedValue,
newState: newState.wrappedValue
)
}
}
Expand Down
Loading