@azerum/ts-csp
    Preparing search index...

    Class Channel<T>

    See methods of ReadableChannel and WritableChannel for details

    Note that undefined cannot be put into the channel, as undefined is used as a special value by ReadableChannel.read

    Type Parameters

    • T extends {} | null

    Implements

    Index

    Constructors

    Properties

    capacity: number

    Capacity of the channel buffer. Integer >= 0. If 0, the channel is unbuffered, meaning that each WritableChannel.write blocks until ReadableChannel.read and vice versa

    Accessors

    • get closed(): boolean

      Returns boolean

    Methods

    • Reads a value from the channel. If there are no values, blocks until there is

      If channel is buffered, takes next value from the buffer. This unblocks first of blocked WritableChannel.write calls if there are any

      If channel is unbuffered, simply unblocks the first of blocked WritableChannel.write

      If the channel is closed and has no values left in the buffer, returns undefined

      Concurrent calls are allowed - each read will get own value (no two reads will get the same value). If multiple calls are blocked, they will unblock one-by-one in unspecified order

      Note: each blocked call occupies memory, and there is no limit on how many calls there can be at once. Typically, programs have a fixed or a finite number of reads, so this should not be a problem

      Returns Promise<undefined | T>

    • Blocks until the channel is "readable", meaning that it either:

      Intuitively, when a channel is "readable", the next ReadableChannel.read call on it will not block

      Note: in combination with ReadableChannel.tryRead, used to implement select

      Type Parameters

      • const U

      Parameters

      • value: U

        Specify value that will be returned once the wait unblocks

      • Optionalsignal: AbortSignal

        Use the signal to cancel the wait. This frees up memory occupied by it. After cancelling, the wait will throw AbortedError

      Returns Promise<U>

    • Writes value to the channel. If there is no free space in the channel, blocks until there is. This gives backpressure: if writer is faster than reader, the channel buffer will eventually fill up and the writer will start to block

      If channel is buffered, tries to write value in the buffer, and blocks if the buffer is full. If channel is unbuffered, waits for ReadableChannel.read call (resolved immediately if there is a blocked ReadableChannel.read call already)

      If the channel was closed before the call, or became closed while the call was blocked, throws CannotWriteIntoClosedChannel

      Order of values is guaranteed for sequential writes: after await ch.write(1); await ch.write(2), 1 is guaranteed to be read before 2. Order is not guaranteed for concurrent writes: after await Promise.race([ch.write(1), ch.write(2)]), 1 and 2 can appear in any order when reading

      Note: in current implementation, order of values is the same as order of calls to write(), so example above will always give 1, 2. This will change in future if worker_threads support will be implemented. It is not advisable to rely on this

      Parameters

      • value: T

      Returns Promise<void>