How to merge and precisely consume an AsyncIterator?

I have a very unique scenario. I have two async sequences that I need to eventually merge. The second sequence comes from a PassthroughPublisher and emits signals from the UI layer. The first sequence comes from some custom logic layer and emits events with different types of data piped through a single sequence. To be able to properly react to every single event at different places, I create an async iterator from the first sequence and call next manually everywhere needed.

At a later point of time the program logic reaches a state where I need to do the following:

  • merge the UI layer stream with the async iterator
  • take and consume the first event
  • it is important to cancel the await without killing the async sequence as the goal is to consume its elements later

I do struggle with this quite a bit. I could go a hacky approach and assume the iterator has reference semantics (which it probably has) and capture it into a new async sequence type to perform the merge operation. It can work, but it feels strange. On top of that, what guarantees me that when the UI sequence emits, we're no longer awaiting the value from the first sequence and silently consuming it?

Another approach on my mind is to merge both async sequences at the start, but disable / filter all UI values until they are expected. Then also filter the values from the iterator and ignore UI events where not needed. This can probably work and it seems less messy.

I'm open for suggestions.

Here is some pseudo code:

let logicSequence = …
let uiSignalSequence = …

var iterator = logicSequence.makeAsyncIterator()

let value = await

// merge `iterator` + `uiSignalSequence`
let mergeSequence = …

// consume one and only the first value from `mergeSequence`.


// consume from iterator again

The idea is not to miss a single value from the iterator.