Class IterableQueueMapper<Element, NewElement>

Accepts queue items via enqueue and calls the mapper on them with specified concurrency, storing the mapper result in a queue of maxUnread size, before being iterated / read by the caller. The enqueue method will block if the queue is full, until an item is read.

Remarks

Typical Use Case

  • Pushing items to an async I/O destination
  • In the simple sequential (concurrency: 1) case, allows 1 item to be flushed async while caller prepares next item
  • Results of the flushed items are needed in a subsequent step (if they are not, use IterableQueueMapperSimple)
  • Prevents the producer from racing ahead of the consumer if maxUnread is reached

Error Handling

The mapper should ideally handle all errors internally to enable error handling closest to where they occur. However, if errors do escape the mapper:

When stopOnMapperError is true (default):

  • First error immediately stops processing
  • Error is thrown from the AsyncIterator's next() call

When stopOnMapperError is false:

  • Processing continues despite errors
  • All errors are collected and thrown together
  • Errors are thrown as AggregateError after all items complete

Usage

  • Items are added to the queue via the await enqueue() method
  • IMPORTANT: await enqueue() method will block until a slot is available, if queue is full
  • Call done() when no more items will be enqueued
  • IMPORTANT: Always await onIdle() to ensure all items are processed

See

IterableMapper for underlying mapper implementation and examples of combined usage

Type Parameters

  • Element

  • NewElement

Hierarchy

  • IterableQueueMapper

Implements

  • AsyncIterable<NewElement>

Constructors

  • Create a new IterableQueueMapper, which uses IterableMapper underneath, and exposes a queue interface for adding items that are not exposed via an iterator.

    Type Parameters

    • Element

    • NewElement

    Parameters

    • mapper: Mapper<Element, NewElement>

      Function called for every enqueued item. Returns a Promise or value.

    • options: IterableMapperOptions = {}

      IterableQueueMapper options

    Returns IterableQueueMapper<Element, NewElement>

    See

Properties

_iterableMapper: IterableMapper<Element, NewElement>
_sourceIterable: IterableQueue<Element>

Methods

  • Returns AsyncIterator<NewElement, any, undefined>

  • Indicate that no more items will be enqueued.

    This releases all readers blocked on enqueue

    Returns void

  • Add an item to the queue, wait if the queue is full.

    Parameters

    • item: Element

      Element to add

    Returns Promise<void>

  • Used by the iterator returned from [Symbol.asyncIterator] Called every time an item is needed

    Returns Promise<IteratorResult<NewElement, any>>

    Iterator result

Generated using TypeDoc