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 specified max size, before being iterated / read by the caller. The enqueue method will block if the queue is full, until an item is read.

Remarks

This allows performing a concurrent mapping with back pressure for items added after queue creation via a method call.

Because items are added via a method call it is possible to chain an IterableMapper that prefetches files and processes them, with an IterableQueueMapper that processes the results of the mapper function of the IterableMapper.

Typical use case is for a background uploader that prevents the producer from racing ahead of the upload process, consuming too much memory or disk space. As items are ready for upload they are added to the queue with the enqueue method, which is awaited by the caller. If the queue has room then enqueue will return immediately, otherwise it will block until there is room.

Type Parameters

  • Element

  • NewElement

Hierarchy

  • IterableQueueMapper

Implements

  • AsyncIterable<NewElement>

Constructors

  • Create a new IterableQueueMapper

    Type Parameters

    • Element

    • NewElement

    Parameters

    • mapper: Mapper<Element, NewElement>

      Function which is called for every item in input. Expected to return a Promise or value.

      The mapper should handle all errors and not allow an error to be thrown out of the mapper function as this enables the best handling of errors closest to the time that they occur.

      If the mapper function does allow an error to be thrown then the stopOnMapperError option controls the behavior: - stopOnMapperError: true - will throw the error out of next or the AsyncIterator returned from [Symbol.asyncIterator] and stop processing. - stopOnMapperError: false - will continue processing and accumulate the errors to be thrown from next or the AsyncIterator returned from [Symbol.asyncIterator] when all items have been processed.

    • options: IterableQueueMapperOptions = {}

      IterableQueueMapper options

    Returns IterableQueueMapper<Element, NewElement>

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