Channel

public final class Channel<Type>

A channel is a synchronization primitive.

Example:

let channel = Channel<Int>()

let coroutine = try Coroutine {
    try channel.send(42, deadline: 1.second.fromNow())
}

let theAnswer = try channel.receive(deadline: 1.second.fromNow())
  • Creates a channel

    Warning

    A channel is a synchronization primitive, not a container. It doesn’t store any items.

    Throws

    The following errors might be thrown:

    VeniceError.canceledCoroutine

    Thrown when the operation is performed within a canceled coroutine.

    VeniceError.outOfMemory

    Thrown when the system doesn’t have enough memory to create a new channel.

    Declaration

    Swift

    public init() throws
  • Reference to the channel which can only send.

    Declaration

    Swift

    public lazy var sending: Sending = Sending(self)
  • Reference to the channel which can only receive.

    Declaration

    Swift

    public lazy var receiving: Receiving = Receiving(self)
  • Sends a value to the channel.

    Declaration

    Swift

    public func send(_ value: Type, deadline: Deadline) throws
  • Sends an error to the channel.

    Declaration

    Swift

    public func send(_ error: Error, deadline: Deadline) throws
  • Receives a value from channel.

    Declaration

    Swift

    @discardableResult public func receive(deadline: Deadline) throws -> Type
  • This function is used to inform the channel that no more send or receive should be performed on the channel.

    Warning

    After done is called on a channel, any attempts to send or receive will result in a VeniceError.doneChannel error.

    Declaration

    Swift

    public func done()
  • Send-only reference to an existing channel.

    Example:

    let channel = Channel<Int>()
    
    func send(to channel: Channel<Int>.Sending) throws {
        try channel.send(42, deadline: 1.second.fromNow())
    }
    
    try send(to: channel.sending)
    

    Declaration

    Swift

    public final class Sending
  • Receive-only reference to an existing channel.

    Example:

    let channel = Channel<Int>()
    
    func receive(from channel: Channel<Int>.Receiving) throws {
        let value = try channel.receive(deadline: 1.second.fromNow())
    }
    
    try receive(from: channel.receiving)
    

    Declaration

    Swift

    public final class Receiving