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
ReadonlycapacityCapacity of the channel buffer. Integer >= 0. If 0, the channel is unbuffered, meaning that each WritableChannel.write blocks until ReadableChannel.read and vice versa
Returns true after WritableChannel.close was called on the channel.
See WritableChannel.close for the explanation of what are closed
channels
Returns the number of currently blocked ReadableChannel.waitUntilReadable calls
Returns the number of currently blocked WritableChannel.waitUntilWritable calls
Closes the channel. Closed channels cannot be written to. They can still be read from if there are values left in the buffer
More precisely, after close:
Blocked calls to WritableChannel.write will unblock by throwing CannotWriteIntoClosedChannel
Future calls to WritableChannel.write will throw CannotWriteIntoClosedChannel immediately
Calls to ReadableChannel.read will consume the values left
in the buffer before returning undefined
Unlike in Go, this method is idempotent
Like ReadableChannel.read, but can be used with select
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
Non-blocking version of ReadableChannel.read. Unlike
ReadableChannel.read, if channel has no values, returns undefined
This means undefined is returned in two cases: (1) the channel is open
but has no values, and the channel is closed and has no values. Use
ReadableChannel.closed to tell those apart
Non-blocking version of WritableChannel.write. Returns true
is the value was written into the channel. Returns false when
WritableChannel.write would block. Throws if the channel
is closed
Blocks until the channel is "readable", meaning that it either:
Intuitively, a channel is "readable" when the next ReadableChannel.read call on it does not block (resolves immediately)
Specify a value to return once the wait unblocks
Optionalsignal: AbortSignalUse signal to cancel the wait. This frees up memory occupied by the wait. After cancelling, the wait will throw AbortedError
Blocks until the channel is "writable", meaning that it either:
Intuitively, a channel is "writable" when the next WritableChannel.write call on it does not block (resolves or rejects immediately)
Specify a value to return once the wait unblocks
Optionalsignal: AbortSignalUse signal to cancel the wait. This frees up memory occupied by the wait. After cancelling, the wait will throw AbortedError
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 give1, 2. This will change in future ifworker_threadssupport will be implemented. It is not advisable to rely on this
Implementation of buffered and unbuffered channel, depending on the constructor parameter
For details, see the methods of ReadableChannel and WritableChannel
Note that
T extends NotUndefined. See NotUndefined docs for the explanation