Options
All
  • Public
  • Public/Protected
  • All
Menu

A Channel extends BaseChannel and provides additional functionality. This includes performing concurrent processing, serving iterators, limiting, etc.

Type parameters

  • T

Hierarchy

Index

Constructors

constructor

  • new Channel<T>(bufferCapacity?: number): Channel<T>
  • Create a new Channel.

    Type parameters

    • T

    Parameters

    • bufferCapacity: number = 0

      The maximum number of items to buffer. Defaults to 0; i.e. all push()/throw() calls will wait for a matching then() call.

    Returns Channel<T>

Properties

Protected _buffer

_buffer: Promise<T>[] = ...

A list of buffered items in the channel

Protected _receivers

_receivers: { reject: (err: any) => unknown; resolve: (value: MaybePromise<T>) => unknown }[] = ...

A list of receivers waiting for an item to be sent

Protected _senders

_senders: { item: Promise<T>; reject: (err: any) => unknown; resolve: () => unknown }[] = ...

List of senders waiting for a receiver / buffer space

Readonly bufferCapacity

bufferCapacity: number = 0

Accessors

bufferSize

  • get bufferSize(): number
  • The number of items currently buffered.

    Returns number

closed

  • get closed(): boolean
  • True if this channel is closed and no longer accepts new values.

    Returns boolean

done

  • get done(): boolean
  • Returns true if this channel is closed and contains no buffered items or waiting senders.

    Returns boolean

onClose

  • get onClose(): Promise<void>
  • A Promise that will resolve when this Channel is closed.

    Returns Promise<void>

Methods

[Symbol.asyncIterator]

  • [Symbol.asyncIterator](): AsyncGenerator<T, any, unknown>

Protected _send

  • _send(item: Promise<T>): Promise<void>
  • Send the given Item. Returns a Promise that resolves when sent.

    Parameters

    • item: Promise<T>

    Returns Promise<void>

clear

  • clear(): Promise<T>[]
  • Clear the channel of all buffered items. Also throws a ChannelClearedError to awaiting senders. Does not close the Channel.

    Returns Promise<T>[]

close

  • close(clear?: boolean): void
  • Close this channel.

    Parameters

    • clear: boolean = false

      Pass true to clear all buffered items / senders when closing the Channel. Defaults to false.

    Returns void

filter

  • filter(onvalue?: null | ((value: T) => MaybePromise<boolean>), onerror?: null | ((error: any) => MaybePromise<boolean>), concurrency?: number, bufferCapacity?: number): Channel<T>
  • Applies the given filter function to the values from this Channel and returns a new Channel with only the filtered values.

    Parameters

    • Optional onvalue: null | ((value: T) => MaybePromise<boolean>)

      A function that takes a value from this Channel and returns a boolean of whether to include the value in the resulting Channel. May return a Promise or a plain value. Defaults to passing all values.

    • Optional onerror: null | ((error: any) => MaybePromise<boolean>)

      A function that takes an error from this Channel and returns a boolean of whether to include the error in the resulting Channel. May return a Promise or a plain value. Defaults to passing all values.

    • Optional concurrency: number

      The number of "coroutines" to spawn to perform this operation. Must be positive and finite. Defaults to 1.

    • Optional bufferCapacity: number

      The buffer size of the output channel. Defaults to 0.

    Returns Channel<T>

forEach

  • forEach(onvalue?: null | ((value: T) => unknown), onerror?: null | ((error: any) => unknown), concurrency?: number): Promise<void>
  • Consumes each value from this Channel, applying the given function on each. Errors on the Channel or in the function will cause the returned Promise to reject.

    Parameters

    • Optional onvalue: null | ((value: T) => unknown)

      A function to invoke with each value from this Channel.

    • Optional onerror: null | ((error: any) => unknown)

      A function to invoke with each error from this Channel.

    • Optional concurrency: number

      The number of "coroutines" to spawn to perform this operation. Must be positive and finite. Defaults to 1.

    Returns Promise<void>

    A Promise that resolves when all values have been consumed, or rejects when an error is received from the Channel.

get

  • get(): Promise<T>
  • Wait for the next value (or error) on this channel.

    Returns Promise<T>

    A Promise that resolves/rejects when the next value (or error) on this channel is emitted.

interrupt

  • interrupt(error: unknown): void
  • Throws the given error to all waiting receivers. Useful if you want to interrupt all waiting routines immediately.

    Parameters

    • error: unknown

    Returns void

map

  • map<U, V>(onvalue?: null | ((value: T) => MaybePromise<U>), onerror?: null | ((error: any) => MaybePromise<V>), concurrency?: number, bufferCapacity?: number): Channel<U | V>
  • Applies the given 1-to-1 mapping function to this Channel and returns a new Channel with the mapped values.

    Type parameters

    • U = T

    • V = never

    Parameters

    • Optional onvalue: null | ((value: T) => MaybePromise<U>)

      A function that maps values from this Channel. To map to an error, either throw or return a rejecting Promise. May return a Promise or a plain value. If omitted, values will be propagated as-is.

    • Optional onerror: null | ((error: any) => MaybePromise<V>)

      A function that maps errors from this Channel to values. To map to an error, either throw or return a rejecting Promise. May return a Promise or a plain value. If omitted, errors will be propagated as-is.

    • Optional concurrency: number

      The number of "coroutines" to spawn to perform this operation. Must be positive and finite. Defaults to 1.

    • Optional bufferCapacity: number

      The buffer size of the output channel. Defaults to 0.

    Returns Channel<U | V>

push

  • push(value: T | PromiseLike<T>): Promise<void>
  • Send a new value over the channel.

    Parameters

    • value: T | PromiseLike<T>

      The value to send, or a Promise resolving to a value.

    Returns Promise<void>

    A Promise that resolves when the value has been successfully pushed.

take

  • Returns a new Channel that reads up to n items from this Channel

    Parameters

    • n: number

      The number of items to read from this Channel

    Returns Channel<T>

then

  • then<U, V>(onvalue?: null | ((value: T) => MaybePromise<U>), onerror?: null | ((error: any) => MaybePromise<V>)): Promise<U | V>
  • Wait for the next value (or error) on this channel and process it. Shorthand for chan.get().then(...).

    Type parameters

    • U = T

    • V = never

    Parameters

    • Optional onvalue: null | ((value: T) => MaybePromise<U>)
    • Optional onerror: null | ((error: any) => MaybePromise<V>)

    Returns Promise<U | V>

throw

  • throw(error: unknown): Promise<void>
  • Throw a new error in the channel. Note that errors are also buffered and subject to buffer capacity.

    Parameters

    • error: unknown

    Returns Promise<void>

    A Promise that resolves when the error has been successfully thrown.

toArray

  • toArray(): Promise<T[]>
  • Consumes the values in this Channel and inserts them into an Array. Returns a Promise that resolves to that Array if no errors were emitted.

    Returns Promise<T[]>

transform

  • transform<U>(func: (input: Channel<T>, output: Channel<U>) => Promise<void>, concurrency?: number, bufferCapacity?: number): Channel<U>
  • Applies a transformation function, applying the transformation to this Channel until it is empty and

    Type parameters

    • U

    Parameters

    • func: (input: Channel<T>, output: Channel<U>) => Promise<void>

      The transformation function. This function may read from the given input channel and write to the given output channel as desired. Because this function should at minimum read from the input channel, and possibly write to the output channel, it should return a Promise in order for concurrency limits to be obeyed.

    • Optional concurrency: number

      The number of "coroutines" to spawn to perform this operation. Must be positive and finite. Defaults to 1.

    • Optional bufferCapacity: number

      The buffer size of the output channel. Defaults to 0.

    Returns Channel<U>

Static from

  • from<T>(source: ArrayLike<MaybePromise<T>> | Iterable<MaybePromise<T>> | AsyncIterable<T>): Channel<T>
  • Creates a new Channel from a given source.

    Type parameters

    • T

    Parameters

    • source: ArrayLike<MaybePromise<T>> | Iterable<MaybePromise<T>> | AsyncIterable<T>

    Returns Channel<T>

Static of

  • of<T>(...values: MaybePromise<T>[]): Channel<T>
  • Creates a new Channel for the given values. A new Channel will be created with these values.

    Type parameters

    • T

    Parameters

    • Rest ...values: MaybePromise<T>[]

      A list of values to be processed. These may be Promises, in which case they will be flattened.

    Returns Channel<T>

Generated using TypeDoc