Skip to content

Commit c0e5874

Browse files
committed
Refactor Effect.task with errors to be reliable.
1 parent a068f52 commit c0e5874

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

Sources/ComposableArchitecture/Beta/Concurrency.swift

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,24 @@ import SwiftUI
8686
operation: @escaping @Sendable () async throws -> Value
8787
) -> Self where Error == Swift.Error {
8888
deferred {
89-
let subject = Signal<Value, Error>.pipe()
90-
let task = Task(priority: priority) {
91-
do {
92-
try Task.checkCancellation()
93-
let output = try await operation()
94-
try Task.checkCancellation()
95-
subject.input.send(value: output)
96-
subject.input.sendCompleted()
97-
} catch is CancellationError {
98-
subject.input.sendCompleted()
99-
} catch {
100-
subject.input.send(error: error)
89+
var task: Task<(), Never>?
90+
let producer = SignalProducer { observer, lifetime in
91+
task = Task(priority: priority) {
92+
do {
93+
try Task.checkCancellation()
94+
let output = try await operation()
95+
try Task.checkCancellation()
96+
observer.send(value: output)
97+
observer.sendCompleted()
98+
} catch is CancellationError {
99+
observer.sendCompleted()
100+
} catch {
101+
observer.send(error: error)
102+
}
101103
}
102104
}
103105

104-
return subject.output.producer
105-
.on(disposed: task.cancel)
106+
return producer.on(disposed: task?.cancel)
106107
}
107108
}
108109
}

Tests/ComposableArchitectureTests/EffectTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ final class EffectTests: XCTestCase {
201201
value: { _ in
202202
XCTFail()
203203
}
204-
).logEvents()
204+
)
205205
.start()
206206

207-
self.wait(for: [expectation], timeout: 0)
207+
self.wait(for: [expectation], timeout: 0)
208208
XCTAssertNotNil(result)
209209
disposable.dispose()
210210
}

0 commit comments

Comments
 (0)