Class Chan<T>Sealed

The concrete realization of Channel

Example

use

function sum(s: Array<number>, c: WriteChannel<number>) {
let sum = 0
for (const v of s) {
sum += v
}
c.write(sum) // send sum to c
}
async function main() {
const s = [7, 2, 8, -9, 4, 0]
const c = new Chan<number>()
sum(s.slice(0, s.length / 2), c)
sum(s.slice(s.length / 2), c)

const [x, y] = [await c.read(), await c.read()] // receive from c

console.log(x, y, x + y)
}
main()

Example

buffered

    const ch = new Chan<number>(2)
ch.write(1)
ch.write(2)
let [v,ok]= ch.readRaw()
console.log(v,ok)
v = ch.read()
console.log(v)

Type Parameters

  • T

Hierarchy

  • Chan

Implements

Constructors

  • Params

    buffered size, if greater than 0 enable buffering for the channel

    Type Parameters

    • T

    Parameters

    • buf: number = 0

    Returns Chan<T>

Properties

closed_: undefined | Chan<any>
never_: undefined | Chan<any>

Accessors

Methods

  • Close the channel, after which the channel will not be able to write, all blocked reads and writes are returned, but the value that has been written to the channel is guaranteed to be fully read

    Returns

    Returns false if the channel has been closed, otherwise closes the channel and returns true

    Returns boolean

  • Attempts to read a value from the channel, returns undefined if no value is readable, returns {done:true} if the channel is closed

    Returns undefined | IteratorResult<T, any>

  • Attempt to write a value to the channel

    Returns

    Returns true if the write is successful

    Throws

    ChannelException Writing a value to a closed channel will throw errChannelClosed

    Parameters

    • val: T

      value to write

    • Optional exception: boolean

      If set to true, writing to a closed channel will throw an exception

    Returns boolean

  • Wait for chan to close, no data will be read from chan

    Returns undefined | Promise<void>

  • Writes a value to the channel, blocking if the channel is not writable until the channel is writable or closed

    Returns

    Returns true if the write is successful, false otherwise this is usually because the channel has been closed

    Throws

    ChannelException Writing a value to a closed channel will throw errChannelClosed

    Parameters

    • val: T

      value to write

    • Optional exception: boolean

      If set to true, writing to a closed channel will throw an exception

    Returns boolean | Promise<boolean>

  • Create a case for select to write to

    Throws

    ChannelException Writing a value to a closed channel, select will throw errChannelClosed

    Parameters

    • val: T

      value to write

    • Optional exception: boolean

      If set to true, writing to a closed channel will throw an exception

    Returns WriteCase<T>

Generated using TypeDoc