@@ -14,7 +14,7 @@ struct DebounceStateMachine<Base: AsyncSequence, C: Clock> {
14
14
typealias Element = Base . Element
15
15
16
16
private enum State {
17
- /// The initial state before a call to `makeAsyncIterator ` happened.
17
+ /// The initial state before a call to `next ` happened.
18
18
case initial( base: Base )
19
19
20
20
/// The state while we are waiting for downstream demand.
@@ -64,24 +64,6 @@ struct DebounceStateMachine<Base: AsyncSequence, C: Clock> {
64
64
self . interval = interval
65
65
}
66
66
67
- /// Actions returned by `iteratorInitialized()`.
68
- enum IteratorInitializedAction {
69
- /// Indicates that a new `Task` should be created that consumes the sequence.
70
- case startTask( Base )
71
- }
72
-
73
- mutating func iteratorInitialized( ) -> IteratorInitializedAction {
74
- switch self . state {
75
- case . initial( let base) :
76
- // This is the first iterator being created and we need to create our `Task`
77
- // that is consuming the upstream sequences.
78
- return . startTask( base)
79
-
80
- case . waitingForDemand, . demandSignalled, . debouncing, . upstreamFailure, . finished:
81
- fatalError ( " debounce allows only a single AsyncIterator to be created " )
82
- }
83
- }
84
-
85
67
/// Actions returned by `iteratorDeinitialized()`.
86
68
enum IteratorDeinitializedAction {
87
69
/// Indicates that the `Task` needs to be cancelled and
@@ -96,8 +78,8 @@ struct DebounceStateMachine<Base: AsyncSequence, C: Clock> {
96
78
mutating func iteratorDeinitialized( ) -> IteratorDeinitializedAction ? {
97
79
switch self . state {
98
80
case . initial:
99
- // An iterator needs to be initialized before it can be deinitialized.
100
- preconditionFailure ( " Internal inconsistency current state \( self . state ) and received iteratorDeinitialized() " )
81
+ // Nothing to do here. No demand was signalled until now
82
+ return . none
101
83
102
84
case . debouncing, . demandSignalled:
103
85
// An iterator was deinitialized while we have a suspended continuation.
@@ -129,16 +111,15 @@ struct DebounceStateMachine<Base: AsyncSequence, C: Clock> {
129
111
}
130
112
}
131
113
132
- mutating func taskStarted( _ task: Task < Void , Never > ) {
114
+ mutating func taskStarted( _ task: Task < Void , Never > , downstreamContinuation : UnsafeContinuation < Result < Element ? , Error > , Never > ) {
133
115
switch self . state {
134
116
case . initial:
135
- // The user called `makeAsyncIterator ` and we are starting the `Task`
117
+ // The user called `next ` and we are starting the `Task`
136
118
// to consume the upstream sequence
137
- self . state = . waitingForDemand (
119
+ self . state = . demandSignalled (
138
120
task: task,
139
- upstreamContinuation: nil ,
140
121
clockContinuation: nil ,
141
- bufferedElement : nil
122
+ downstreamContinuation : downstreamContinuation
142
123
)
143
124
144
125
case . debouncing, . demandSignalled, . waitingForDemand, . upstreamFailure, . finished:
@@ -652,6 +633,8 @@ struct DebounceStateMachine<Base: AsyncSequence, C: Clock> {
652
633
653
634
/// Actions returned by `next()`.
654
635
enum NextAction {
636
+ /// Indicates that a new `Task` should be created that consumes the sequence.
637
+ case startTask( Base )
655
638
case resumeUpstreamContinuation(
656
639
upstreamContinuation: UnsafeContinuation < Void , Error > ?
657
640
)
@@ -671,8 +654,10 @@ struct DebounceStateMachine<Base: AsyncSequence, C: Clock> {
671
654
672
655
mutating func next( for continuation: UnsafeContinuation < Result < Element ? , Error > , Never > ) -> NextAction {
673
656
switch self . state {
674
- case . initial:
675
- preconditionFailure ( " Internal inconsistency current state \( self . state) and received next() " )
657
+ case . initial( let base) :
658
+ // This is the first time we get demand singalled so we have to start the task
659
+ // The transition to the next state is done in the taskStarted method
660
+ return . startTask( base)
676
661
677
662
case . demandSignalled, . debouncing:
678
663
// We already got demand signalled and have suspended the downstream task
0 commit comments