Skip to main content

Home > flowp > Channel

Channel class

Promise based multi producer single consumer channel

  • buffered message queue

  • send / receive basic message passing

  • pipe piping to other channels (or use pipe.to())

  • stream ES6 async iterator api

  • freeze temporarily block all consumers, useful if your target has limited rate of consumption like Node.js net.Socket

Signature:

export declare class Channel<T> implements PipeSource<T>, PipeTarget<T> 

Implements: PipeSource<T>, PipeTarget<T>

Constructors

ConstructorModifiersDescription
(constructor)(capacity)create a new channel with specified capacity

Properties

PropertyModifiersTypeDescription
capacitynumberGet the number of maximum items in queue
ChannelFullErrorstatictypeof ChannelFullError
class ChannelFullError extends Error

| | closed | | boolean | check if channel has been closed | | ClosedChannelError | static | typeof ClosedChannelError |

class ClosedChannelError extends Error

| | size | | number | Get the number of current queued items |

Methods

MethodModifiersDescription
close()close the channel, future send will throw a Channel.ClosedChannelError
pause()

stop Channel.stream() / Channel.pipe() / Channel.receive() new items until Channel.resume() is called

items sending to the channel will be queued despite pipe enabled

pipe(target, options)

pipe channel output to target

there is only one target at the same time, use ChannelHub if you want to have multiple readers

receive()

retrieve a value from channel.

will never resolve if Channel.pipe() or is enabled; will race with Channel.stream()

resume()resume the channel so Channel.stream() / Channel.pipe() / Channel.receive() can continue to handle new messages
send(value)

send a value to channel.

if the channel has reached its capacity, then call to send will be blocked

sendAsync(value)send a promise to channel, after the promise is resolved, send its fullfilled value
stream()get a stream to read from the channel, internally uses Channel.receive()
tryReceive()try receive one message
trySend(value)try to send a value synchronosly
unpipe()unlink output with target, future input will be stored in channel's internal buffer