Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/chan.ts>Channel

A JavaScript extension package for building strong and modern applications.
Latest
class Channel
implements AsyncIterable<T>
import { Channel } from "https://deno.land/x/ayonli_jsext@v0.9.72/chan.ts";

A channel implementation that transfers data across routines, even across multiple threads, inspired by Golang.

Constructors

new
Channel(capacity?)

Properties

private
buffer: T[]
private
consumers: ((err: Error | null, data?: T) => void)[]
private
optional
error: Error | null
private
producers: (() => T)[]
private
state: 0 | 1 | 2
readonly
capacity: number

The capacity is the maximum number of data allowed to be buffered.

readonly
[id]

Methods

close(err?: Error | null)

Closes the channel. If err is supplied, it will be captured by the receiver.

No more data shall be sent once the channel is closed.

Explicitly closing the channel is not required, if the channel is no longer used, it will be automatically released by the GC. However, if the channel is used in a for await...of... loop, closing the channel will allow the loop to break automatically.

Moreover, if the channel is used between parallel threads, it will no longer be able to release automatically, must explicitly call this function in order to release for GC.

deprecated
pop(): Promise<T | undefined>
deprecated
push(data: T): Promise<void>
recv(): Promise<T | undefined>

Retrieves data from the channel.

If there isn't data available at the moment, this function will block until new data is available.

If the channel is closed, then:

  • If there is error set in the channel, this function throws that error immediately.
  • Otherwise, this function returns undefined immediately.
send(data: T): Promise<void>

Pushes data to the channel.

If there is a receiver, the data will be consumed immediately. Otherwise:

  • If this is an non-buffered channel, this function will block until a receiver is available and the data is consumed.

  • If this is a buffered channel, then:

    • If the buffer size is within the capacity, the data will be pushed to the buffer.
    • Otherwise, this function will block until there is new space for the data in the buffer.
[Symbol.asyncIterator](): { next(): Promise<IteratorResult<T>>; }