Tolerate cancelling before we create our task (#250)

# Motivation
We switched the point in time when we create tasks to the first call to `next()`. This had the side effect that we can now be cancelled before we even created our task. Which by itself is fine but we didn't handle it in the state machines.

# Modification
Adapt the state machines of `debounce`, `zip`, `merge` and `combineLatest` to handle cancellation before `next()`.

# Result
We shouldn't run into these preconditions anymore.
This commit is contained in:
Franz Busch authored and GitHub committed 2023-02-06 17:05:07 +00:00
1 parent bf0ec24bde
commit ca2ac60495
8 files changed
+54 -8

No files matched your search

@@ -585,7 +585,9 @@ struct CombineLatestStateMachine<
mutating func cancelled() -> CancelledAction? {
switch self.state {
case .initial:
preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()")
state = .finished
return .none
case .waitingForDemand(let task, let upstreams, _):
// The downstream task got cancelled so we need to cancel our upstream Task
@@ -583,9 +583,8 @@ struct DebounceStateMachine<Base: AsyncSequence, C: Clock> {
mutating func cancelled() -> CancelledAction? {
switch self.state {
case .initial:
// Since we are transitioning to `merging` before we return from `makeAsyncIterator`
// this can never happen
preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()")
state = .finished
return .none
case .waitingForDemand:
// We got cancelled before we event got any demand. This can happen if a cancelled task
@@ -465,9 +465,11 @@ struct MergeStateMachine<
mutating func cancelled() -> CancelledAction {
switch state {
case .initial:
// Since we are transitioning to `merging` before we return from `makeAsyncIterator`
// this can never happen
preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()")
// Since we are only transitioning to merging when the task is started we
// can be cancelled already.
state = .finished
return .none
case let .merging(task, _, upstreamContinuations, _, .some(downstreamContinuation)):
// The downstream Task got cancelled so we need to cancel our upstream Task
@@ -460,7 +460,9 @@ struct ZipStateMachine<
mutating func cancelled() -> CancelledAction? {
switch self.state {
case .initial:
preconditionFailure("Internal inconsistency current state \(self.state) and received cancelled()")
state = .finished
return .none
case .waitingForDemand(let task, let upstreams):
// The downstream task got cancelled so we need to cancel our upstream Task