|
| 1 | +// |
| 2 | +// AsyncSequence+WithLatestFrom.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Thibault Wittemberg on 07/03/2022. |
| 6 | +// |
| 7 | + |
| 8 | +public extension AsyncSequence { |
| 9 | + /// Merges two AsyncSequences into a single one by combining each value |
| 10 | + /// from self with the latest value from the other sequence, if any. |
| 11 | + /// |
| 12 | + /// ``` |
| 13 | + /// let seq1 = AsyncStreams.CurrentValue<Int>(1) |
| 14 | + /// let seq2 = AsyncStreams.CurrentValue<String>("1") |
| 15 | + /// |
| 16 | + /// let combinedSeq = seq1.withLatestFrom(seq2) |
| 17 | + /// |
| 18 | + /// Task { |
| 19 | + /// for try await element in combinedSeq { |
| 20 | + /// print(element) |
| 21 | + /// } |
| 22 | + /// } |
| 23 | + /// |
| 24 | + /// seq1.send(2) |
| 25 | + /// seq2.send("2") |
| 26 | + /// seq1.send(3) |
| 27 | + /// |
| 28 | + /// // will print: |
| 29 | + /// (1, "1") |
| 30 | + /// (2, "1") |
| 31 | + /// (3, "2") |
| 32 | + /// ``` |
| 33 | + /// |
| 34 | + /// - parameter other: the other async sequence |
| 35 | + /// |
| 36 | + /// - returns: An async sequence emitting the result of combining each value of the self |
| 37 | + /// with the latest value from the other sequence. If the other sequence finishes, the returned sequence |
| 38 | + /// will finish with the next value from self. |
| 39 | + /// |
| 40 | + func withLatestFrom<OtherAsyncSequence: AsyncSequence>( |
| 41 | + _ other: OtherAsyncSequence, |
| 42 | + otherPriority: TaskPriority? = nil |
| 43 | + ) -> AsyncWithLatestFromSequence<Self, OtherAsyncSequence> { |
| 44 | + AsyncWithLatestFromSequence( |
| 45 | + self, |
| 46 | + other: other, |
| 47 | + otherPriority: otherPriority |
| 48 | + ) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +public struct AsyncWithLatestFromSequence<UpstreamAsyncSequence: AsyncSequence, OtherAsyncSequence: AsyncSequence>: AsyncSequence { |
| 53 | + public typealias Element = (UpstreamAsyncSequence.Element, OtherAsyncSequence.Element) |
| 54 | + public typealias AsyncIterator = Iterator |
| 55 | + |
| 56 | + let upstreamAsyncSequence: UpstreamAsyncSequence |
| 57 | + let otherAsyncSequence: OtherAsyncSequence |
| 58 | + let otherPriority: TaskPriority? |
| 59 | + |
| 60 | + public init( |
| 61 | + _ upstreamAsyncSequence: UpstreamAsyncSequence, |
| 62 | + other otherAsyncSequence: OtherAsyncSequence, |
| 63 | + otherPriority: TaskPriority? = nil |
| 64 | + ) { |
| 65 | + self.upstreamAsyncSequence = upstreamAsyncSequence |
| 66 | + self.otherAsyncSequence = otherAsyncSequence |
| 67 | + self.otherPriority = otherPriority |
| 68 | + } |
| 69 | + |
| 70 | + public func makeAsyncIterator() -> AsyncIterator { |
| 71 | + Iterator( |
| 72 | + self.upstreamAsyncSequence.makeAsyncIterator(), |
| 73 | + other: self.otherAsyncSequence.makeAsyncIterator(), |
| 74 | + otherPriority: self.otherPriority |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + final class OtherIteratorManager { |
| 79 | + var otherElement: OtherAsyncSequence.Element? |
| 80 | + var otherIterator: OtherAsyncSequence.AsyncIterator |
| 81 | + var hasStarted = false |
| 82 | + |
| 83 | + let otherPriority: TaskPriority? |
| 84 | + |
| 85 | + init( |
| 86 | + otherIterator: OtherAsyncSequence.AsyncIterator, |
| 87 | + otherPriority: TaskPriority? |
| 88 | + ) { |
| 89 | + self.otherIterator = otherIterator |
| 90 | + self.otherPriority = otherPriority |
| 91 | + } |
| 92 | + |
| 93 | + /// iterates over the other sequence and track its current value |
| 94 | + func startOtherIterator() async throws { |
| 95 | + guard !self.hasStarted else { return } |
| 96 | + self.hasStarted = true |
| 97 | + |
| 98 | + self.otherElement = try await self.otherIterator.next() |
| 99 | + |
| 100 | + Task(priority: self.otherPriority) { [weak self] in |
| 101 | + while let element = try await self?.otherIterator.next() { |
| 102 | + guard !Task.isCancelled else { break } |
| 103 | + |
| 104 | + self?.otherElement = element |
| 105 | + } |
| 106 | + self?.otherElement = nil |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public struct Iterator: AsyncIteratorProtocol { |
| 112 | + var upstreamAsyncIterator: UpstreamAsyncSequence.AsyncIterator |
| 113 | + let otherIteratorManager: OtherIteratorManager |
| 114 | + |
| 115 | + init( |
| 116 | + _ upstreamAsyncIterator: UpstreamAsyncSequence.AsyncIterator, |
| 117 | + other otherAsyncIterator: OtherAsyncSequence.AsyncIterator, |
| 118 | + otherPriority: TaskPriority? |
| 119 | + ) { |
| 120 | + self.upstreamAsyncIterator = upstreamAsyncIterator |
| 121 | + self.otherIteratorManager = OtherIteratorManager( |
| 122 | + otherIterator: otherAsyncIterator, |
| 123 | + otherPriority: otherPriority |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + public mutating func next() async throws -> Element? { |
| 128 | + guard !Task.isCancelled else { return nil } |
| 129 | + |
| 130 | + try await self.otherIteratorManager.startOtherIterator() |
| 131 | + |
| 132 | + let upstreamElement = try await self.upstreamAsyncIterator.next() |
| 133 | + let otherElement = self.otherIteratorManager.otherElement |
| 134 | + |
| 135 | + guard let nonNilUpstreamElement = upstreamElement, |
| 136 | + let nonNilOtherElement = otherElement else { |
| 137 | + return nil |
| 138 | + } |
| 139 | + |
| 140 | + return (nonNilUpstreamElement, nonNilOtherElement) |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments