@@ -82,27 +82,6 @@ struct MergeStateMachine<
82
82
}
83
83
}
84
84
85
- /// Actions returned by `iteratorInitialized()`.
86
- enum IteratorInitializedAction {
87
- /// Indicates that a new `Task` should be created that consumed the sequences.
88
- case startTask( Base1 , Base2 , Base3 ? )
89
- }
90
-
91
- mutating func iteratorInitialized( ) -> IteratorInitializedAction {
92
- switch state {
93
- case let . initial( base1, base2, base3) :
94
- // This is the first iterator being created and we need to create our `Task`
95
- // that is consuming the upstream sequences.
96
- return . startTask( base1, base2, base3)
97
-
98
- case . merging, . upstreamFailure, . finished:
99
- fatalError ( " merge allows only a single AsyncIterator to be created " )
100
-
101
- case . modifying:
102
- preconditionFailure ( " Invalid state " )
103
- }
104
- }
105
-
106
85
/// Actions returned by `iteratorDeinitialized()`.
107
86
enum IteratorDeinitializedAction {
108
87
/// Indicates that the `Task` needs to be cancelled and
@@ -118,8 +97,8 @@ struct MergeStateMachine<
118
97
mutating func iteratorDeinitialized( ) -> IteratorDeinitializedAction {
119
98
switch state {
120
99
case . initial:
121
- // An iterator needs to be initialized before it can be deinitialized.
122
- preconditionFailure ( " Internal inconsistency current state \( self . state ) and received iteratorDeinitialized() " )
100
+ // Nothing to do here. No demand was signalled until now
101
+ return . none
123
102
124
103
case . merging( _, _, _, _, . some) :
125
104
// An iterator was deinitialized while we have a suspended continuation.
@@ -531,6 +510,8 @@ struct MergeStateMachine<
531
510
532
511
/// Actions returned by `next()`.
533
512
enum NextAction {
513
+ /// Indicates that a new `Task` should be created that consumes the sequence and the downstream must be supsended
514
+ case startTaskAndSuspendDownstreamTask( Base1 , Base2 , Base3 ? )
534
515
/// Indicates that the `element` should be returned.
535
516
case returnElement( Result < Element , Error > )
536
517
/// Indicates that `nil` should be returned.
@@ -543,8 +524,10 @@ struct MergeStateMachine<
543
524
544
525
mutating func next( ) -> NextAction {
545
526
switch state {
546
- case . initial:
547
- preconditionFailure ( " Internal inconsistency current state \( self . state) and received next() " )
527
+ case . initial( let base1, let base2, let base3) :
528
+ // This is the first time we got demand signalled. We need to start the task now
529
+ // We are transitioning to merging in the taskStarted method.
530
+ return . startTaskAndSuspendDownstreamTask( base1, base2, base3)
548
531
549
532
case . merging( _, _, _, _, . some) :
550
533
// We have multiple AsyncIterators iterating the sequence
0 commit comments