|
| 1 | +// |
| 2 | +// AsyncSequence+Share.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Thibault Wittemberg on 03/03/2022. |
| 6 | +// |
| 7 | + |
| 8 | +public extension AsyncSequence { |
| 9 | + /// Shares the output of an upstream async sequence with multiple client loops. |
| 10 | + /// |
| 11 | + /// - Tip: ``share()`` is effectively a shortcut for ``multicast()`` using a ``Passthrough`` stream, with an implicit ``autoconnect()``. |
| 12 | + /// |
| 13 | + /// The following example uses an async sequence as a counter to emit three random numbers. Each element is delayed by 1s to give the seconf loop a chance to |
| 14 | + /// catch all the values. |
| 15 | + /// |
| 16 | + /// ``` |
| 17 | + /// let sharedAsyncSequence = AsyncSequences.From(["first", "second", "third"], interval: .seconds(1)) |
| 18 | + /// .map { ($0, Int.random(in: 0...100)) } |
| 19 | + /// .handleEvents(onElement: { print("AsyncSequence produces: \($0)") }) |
| 20 | + /// .share() |
| 21 | + /// |
| 22 | + /// Task { |
| 23 | + /// try await sharedAsyncSequence |
| 24 | + /// .collect { print ("Task 1 received: \($0)") } |
| 25 | + /// } |
| 26 | + /// |
| 27 | + /// Task { |
| 28 | + /// try await sharedAsyncSequence |
| 29 | + /// .collect { print ("Task 2 received: \($0)") } |
| 30 | + /// } |
| 31 | + /// |
| 32 | + /// // will print: |
| 33 | + /// // AsyncSequence produces: ("First", 78) |
| 34 | + /// // Stream 2 received: ("First", 78) |
| 35 | + /// // Stream 1 received: ("First", 78) |
| 36 | + /// // AsyncSequence produces: ("Second", 98) |
| 37 | + /// // Stream 2 received: ("Second", 98) |
| 38 | + /// // Stream 1 received: ("Second", 98) |
| 39 | + /// // AsyncSequence produces: ("Third", 61) |
| 40 | + /// // Stream 2 received: ("Third", 61) |
| 41 | + /// // Stream 1 received: ("Third", 61) |
| 42 | + /// ``` |
| 43 | + /// In this example, the output shows that the upstream async sequence produces each random value only one time, and then sends the value to both client loops. |
| 44 | + /// |
| 45 | + /// Without the ``share()`` operator, loop 1 receives three random values, followed by loop 2 receiving three different random values. |
| 46 | + /// |
| 47 | + /// Also note that ``AsyncShareSequence`` is a class rather than a structure like most other publishers. |
| 48 | + /// This means you can use this operator to create a publisher instance that uses reference semantics. |
| 49 | + /// - Returns: A class instance that shares elements received from its upstream async sequence to multiple client loops. |
| 50 | + func share() -> AsyncSequences.AsyncShareSequence<Self> { |
| 51 | + let stream = AsyncStreams.Passthrough<Element>() |
| 52 | + return self.multicast(stream).autoconnect() |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +extension AsyncSequences { |
| 57 | + public typealias AsyncShareSequence<UpstreamAsyncSequence: AsyncSequence> = |
| 58 | + AsyncMulticastSequence<UpstreamAsyncSequence,AsyncStreams.Passthrough<UpstreamAsyncSequence.Element>> |
| 59 | +} |
0 commit comments