Options
All
  • Public
  • Public/Protected
  • All
Menu

An IteratorChannel automatically emits values from an (async-)iterable source. It uses a pull-based mechanism for fetching the values -- i.e. iteration is not started until the first get() call is made.

Type parameters

  • T

Hierarchy

Index

Constructors

constructor

  • new IteratorChannel<T>(source: Iterable<MaybePromise<T>> | AsyncIterable<T>, limit?: number): IteratorChannel<T>
  • Create a new IteratorChannel.

    Type parameters

    • T

    Parameters

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

      the iterable source to take elements from.

    • limit: number = ...

      An optional maximum number of items to take from the source before closing this Channel.

    Returns IteratorChannel<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>[]

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>

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>

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>

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