SharedAsyncSequence

public struct SharedAsyncSequence<Base> : AsyncSequence where Base : AsyncSequence

An async sequence that can be shared between multiple tasks.

let values = [
    "a",
    "ab",
    "abc",
    "abcd"
]

let stream = AsyncStream { continuation in
    for value in values {
        continuation.yield(value)
    }
    continuation.finish()
}
.shared()

Task {
    let values = try await self.stream.collect()
    // ...
}

Task.detached {
    let values = try await self.stream.collect()
    // ...
}

let values = try await self.stream.collect()
// ...
  • The type of async iterator.

    Declaration

    Swift

    public typealias AsyncIterator = AsyncThrowingStream<Base.Element, Error>.Iterator
  • The type of elements streamed.

    Declaration

    Swift

    public typealias Element = Base.Element

SharedAsyncSequence (Public Properties)

  • Creates a shareable async sequence that can be used across multiple tasks.

    Declaration

    Swift

    public init(_ base: Base)

    Parameters

    base

    The async sequence in which this sequence receives it’s elements.

AsyncSequence

  • Creates an async iterator that emits elements of this async sequence.

    Declaration

    Swift

    public func makeAsyncIterator() -> AsyncThrowingStream<Base.Element, Error>.Iterator

    Return Value

    An instance that conforms to AsyncIteratorProtocol.

CurrentElementAsyncSequence extension

  • yield(_:) Asynchronous

    Yield a new element to the sequence.

    Yielding a new element will update this async sequence’s element property along with emitting it through the sequence.

    Declaration

    Swift

    public func yield<Element>(_ element: Element) async where Base == CurrentElementAsyncSequence<Element>

    Parameters

    element

    The element to yield.

  • finish() Asynchronous

    Mark the sequence as finished by having it’s iterator emit nil.

    Once finished, any calls to yield will result in no change.

    Declaration

    Swift

    public func finish<Element>() async where Base == CurrentElementAsyncSequence<Element>
  • finish(with:) Asynchronous

    Emit one last element beford marking the sequence as finished by having it’s iterator emit nil.

    Once finished, any calls to yield will result in no change.

    Declaration

    Swift

    public func finish<Element>(with element: Element) async where Base == CurrentElementAsyncSequence<Element>

    Parameters

    element

    The element to emit.

  • element() Asynchronous

    The element wrapped by this async sequence, emitted as a new element whenever it changes.

    Declaration

    Swift

    public func element<Element>() async -> Element where Base == CurrentElementAsyncSequence<Element>

PassthroughAsyncSequence extension

  • Yield a new element to the sequence.

    Yielding a new element will emit it through the sequence.

    Declaration

    Swift

    public func yield<Element>(_ element: Element) where Base == PassthroughAsyncSequence<Element>

    Parameters

    element

    The element to yield.

  • Mark the sequence as finished by having it’s iterator emit nil.

    Once finished, any calls to yield will result in no change.

    Declaration

    Swift

    public func finish<Element>() where Base == PassthroughAsyncSequence<Element>
  • Emit one last element beford marking the sequence as finished by having it’s iterator emit nil.

    Once finished, any calls to yield will result in no change.

    Declaration

    Swift

    public func finish<Element>(with element: Element) where Base == PassthroughAsyncSequence<Element>

    Parameters

    element

    The element to emit.