CoChannel
public final class CoChannel<Element>extension CoChannel: CoCancellableChannel 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
Alwaysclose() 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")
}
- 
                  
                  Initializes a channel. DeclarationSwift public init(bufferType type: BufferType = .unlimited)ParameterstypeThe type of channel buffer. 
- 
                  
                  Initializes a channel with BufferType.buffered(capacity:).DeclarationSwift public init(capacity: Int)ParameterscapacityThe maximum number of elements that can be stored in a channel. 
- 
                  
                  The type of channel buffer. DeclarationSwift @inlinable public var bufferType: BufferType { get }
- 
                  
                  A CoChannelwrapper that provides send-only functionality.DeclarationSwift @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.DeclarationSwift @inlinable public func awaitSend(_ element: Element) throwsParameterselementValue 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. DeclarationSwift @discardableResult @inlinable public func offer(_ element: Element) -> BoolParameterselementValue that might be sent to the channel. Return Valuetrueif sent successfully orfalseif channel buffer is full or channel is closed or canceled.
- 
                  
                  A CoChannelwrapper that provides receive-only functionality.DeclarationSwift @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.DeclarationSwift @inlinable public func awaitReceive() throws -> ElementReturn ValueRemoved value from the channel. 
- 
                  
                  Retrieves and removes an element from this channel. DeclarationSwift @inlinable public func poll() -> Element?Return ValueElement 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. DeclarationSwift @inlinable public func whenReceive(_ callback: @escaping (Result<Element, CoChannelError>) -> Void)ParameterscallbackThe callback that is called when a value is received. 
- 
                  
                  Returns a number of elements in this channel. DeclarationSwift @inlinable public var count: Int { get }
- 
                  
                  Returns trueif the channel is empty (contains no elements), which means no elements to receive.DeclarationSwift @inlinable public var isEmpty: Bool { get }
- 
                  
                  Closes this channel. No more send should be performed on the channel. DeclarationSwift @discardableResult @inlinable public func close() -> BoolReturn Valuetrueif closed successfully orfalseif channel is already closed or canceled.
- 
                  
                  Returns trueif the channel is closed.DeclarationSwift @inlinable public var isClosed: Bool { get }
- 
                  
                  Closes the channel and removes all buffered sent elements from it. DeclarationSwift @inlinable public func cancel()
- 
                  
                  Returns trueif the channel is canceled.DeclarationSwift @inlinable public var isCanceled: Bool { get }
- 
                  
                  Adds an observer callback that is called when the CoChannelis canceled.DeclarationSwift @inlinable public func whenCanceled(_ callback: @escaping () -> Void)ParameterscallbackThe callback that is called when the CoChannelis canceled.
- 
                  
                  Adds an observer callback that is called when the CoChannelis completed (closed, canceled or deinited).DeclarationSwift @inlinable public func whenComplete(_ callback: @escaping () -> Void)ParameterscallbackThe callback that is called when the CoChannelis completed.
- 
                  
                  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.DeclarationSwift @inlinable public func makeIterator() -> AnyIterator<Element>Return ValueIterator for the channel elements. 
- 
                  
                  Declaration
- 
                  
                  Declaration
- 
                  
                  Returns a publisher that emits elements of this CoChannel.DeclarationSwift @inlinable public func publisher() -> AnyPublisher<Element, CoChannelError>
 CoChannel Class Reference
        CoChannel Class Reference