CoChannel

public final class CoChannel<Element>
extension CoChannel: CoCancellable

Channel is a non-blocking primitive for communication between a sender and a receiver. Conceptually, a channel is similar to a queue that allows to suspend a coroutine on receive if it is empty or on send if it is full.

Important

Always close() or cancel() a channel when you are done to resume all suspended coroutines by the channel.
let channel = CoChannel<Int>(capacity: 1)

DispatchQueue.global().startCoroutine {
   for i in 0..<100 {
       try channel.awaitSend(i)
   }
   channel.close()
}

DispatchQueue.global().startCoroutine {
    for i in channel.makeIterator() {
        print("Receive", i)
    }
    print("Done")
}

send

  • A CoChannel wrapper that provides send-only functionality.

    Declaration

    Swift

    @inlinable
    public var sender: Sender { get }
  • Sends the element to this channel, suspending the coroutine while the buffer of this channel is full. Must be called inside a coroutine.

    Throws

    CoChannelError when canceled or closed.

    Declaration

    Swift

    @inlinable
    public func awaitSend(_ element: Element) throws

    Parameters

    element

    Value that will be sent to the channel.

  • Adds the future’s value to this channel when it will be available.

    Declaration

    Swift

    @inlinable
    public func sendFuture(_ future: CoFuture<Element>)

    Parameters

    future

    CoFuture‘s value that will be sent to the channel.

  • Immediately adds the value to this channel, if this doesn’t violate its capacity restrictions, and returns true. Otherwise, just returns false.

    Declaration

    Swift

    @discardableResult
    @inlinable
    public func offer(_ element: Element) -> Bool

    Parameters

    element

    Value that might be sent to the channel.

    Return Value

    true if sent successfully or false if channel buffer is full or channel is closed or canceled.

receive

  • A CoChannel wrapper that provides receive-only functionality.

    Declaration

    Swift

    @inlinable
    public var receiver: Receiver { get }
  • Retrieves and removes an element from this channel if it’s not empty, or suspends a coroutine while the channel is empty.

    Throws

    CoChannelError when canceled or closed.

    Declaration

    Swift

    @inlinable
    public func awaitReceive() throws -> Element

    Return Value

    Removed value from the channel.

  • Creates CoFuture with retrieved value from this channel.

    Declaration

    Swift

    @inlinable
    public func receiveFuture() -> CoFuture<Element>

    Return Value

    CoFuture with a future value from the channel.

  • Retrieves and removes an element from this channel.

    Declaration

    Swift

    @inlinable
    public func poll() -> Element?

    Return Value

    Element from this channel if its not empty, or returns nill if the channel is empty or is closed or canceled.

  • Adds an observer callback to receive an element from this channel.

    Declaration

    Swift

    @inlinable
    public func whenReceive(_ callback: @escaping (Result<Element, CoChannelError>) -> Void)

    Parameters

    callback

    The callback that is called when a value is received.

  • Returns a number of elements in this channel.

    Declaration

    Swift

    @inlinable
    public var count: Int { get }
  • Returns true if the channel is empty (contains no elements), which means no elements to receive.

    Declaration

    Swift

    @inlinable
    public var isEmpty: Bool { get }

map

  • Returns new Receiver that provides transformed values from this CoChannel.

    Declaration

    Swift

    @inlinable
    public func map<T>(_ transform: @escaping (Element) -> T) -> CoChannel<T>.Receiver

    Parameters

    transform

    A mapping closure.

    Return Value

    A Receiver with transformed values.

close

  • Closes this channel. No more send should be performed on the channel.

    Declaration

    Swift

    @discardableResult
    @inlinable
    public func close() -> Bool

    Return Value

    true if closed successfully or false if channel is already closed or canceled.

  • Returns true if the channel is closed.

    Declaration

    Swift

    @inlinable
    public var isClosed: Bool { get }

cancel

  • Closes the channel and removes all buffered sent elements from it.

    Declaration

    Swift

    @inlinable
    public func cancel()
  • Returns true if the channel is canceled.

    Declaration

    Swift

    @inlinable
    public var isCanceled: Bool { get }
  • Adds an observer callback that is called when the CoChannel is canceled.

    Declaration

    Swift

    @inlinable
    public func whenCanceled(_ callback: @escaping () -> Void)

    Parameters

    callback

    The callback that is called when the CoChannel is canceled.

complete

  • Adds an observer callback that is called when the CoChannel is completed (closed, canceled or deinited).

    Declaration

    Swift

    @inlinable
    public func whenComplete(_ callback: @escaping () -> Void)

    Parameters

    callback

    The callback that is called when the CoChannel is completed.

sequence

  • Make an iterator which successively retrieves and removes values from the channel.

    If next() was called inside a coroutine and there are no more elements in the channel, then the coroutine will be suspended until a new element will be added to the channel or it will be closed or canceled.

    Declaration

    Swift

    @inlinable
    public func makeIterator() -> AnyIterator<Element>

    Return Value

    Iterator for the channel elements.

  • A CoChannel wrapper that provides receive-only functionality.

    See more

    Declaration

    Swift

    public class Receiver
    extension CoChannel.Receiver: CoCancellable
  • A CoChannel wrapper that provides send-only functionality.

    See more

    Declaration

    Swift

    public final class Sender
    extension CoChannel.Sender: CoCancellable

publisher

  • Returns a publisher that emits elements of this CoChannel.

    Declaration

    Swift

    @inlinable
    public func publisher() -> AnyPublisher<Element, CoChannelError>