Skip to content

Commit 5cd90c3

Browse files
Propagate current navigation path to cancellable publisher effects (#3728)
* Propagate navigation ID to escaping closure in cancellation. * Add test * wip * wip --------- Co-authored-by: Brandon Williams <mbrandonw@hey.com>
1 parent 8730c8f commit 5cd90c3

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

Sources/ComposableArchitecture/Effects/Cancellation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extension Effect {
4242
case let .publisher(publisher):
4343
return Self(
4444
operation: .publisher(
45-
Deferred {
45+
Deferred { [navigationIDPath]
4646
()
4747
-> Publishers.HandleEvents<
4848
Publishers.PrefixUntilOutput<

Tests/ComposableArchitectureTests/EffectCancellationTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,5 +432,37 @@ final class EffectCancellationTests: BaseTCATestCase {
432432
// structs in Swift have the same hash value.
433433
XCTAssertNotEqual(id1.hashValue, id2.hashValue)
434434
}
435+
436+
func testCancellablePath() async throws {
437+
let navigationIDPath = NavigationIDPath(path: [NavigationID()])
438+
let effect = withDependencies {
439+
$0.navigationIDPath = navigationIDPath
440+
} operation: {
441+
Effect
442+
.publisher {
443+
Just(()).delay(for: .seconds(1), scheduler: DispatchQueue(label: #function))
444+
}
445+
.cancellable(id: 1)
446+
}
447+
await withThrowingTaskGroup(of: Void.self) { taskGroup in
448+
taskGroup.addTask {
449+
await withDependencies {
450+
$0.navigationIDPath = NavigationIDPath(path: [NavigationID()])
451+
} operation: {
452+
for await _ in effect.actions {
453+
XCTFail()
454+
}
455+
}
456+
}
457+
taskGroup.addTask {
458+
try await withDependencies {
459+
$0.navigationIDPath = navigationIDPath
460+
} operation: {
461+
try await Task.sleep(nanoseconds: NSEC_PER_SEC / 2)
462+
Task.cancel(id: 1)
463+
}
464+
}
465+
}
466+
}
435467
}
436468
#endif

0 commit comments

Comments
 (0)