Class IterableMapper<Element, NewElement>

Iterates over a source iterable with specified concurrency, calling the mapper on each iterated item, and storing the mapper result in a queue of specified max size, before being iterated / read by the caller.

Remarks

This allows performing a concurrent mapping with back pressure (won't iterate all source items if the consumer is not reading).

Typical use case is for a prefetcher that ensures that items are always ready for the consumer but that large numbers of items are not processed before the consumer is ready for them.

Type Parameters

  • Element

  • NewElement

Hierarchy

  • IterableMapper

Implements

  • AsyncIterable<NewElement>

Constructors

  • Create a new IterableMapper

    Type Parameters

    • Element

    • NewElement

    Parameters

    • input: AsyncIterable<Element> | Iterable<Element>

      Iterated over concurrently in the mapper function.

    • 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: IterableMapperOptions = {}

      IterableMapper options

    Returns IterableMapper<Element, NewElement>

Properties

_activeRunners: number = 0
_asyncIterator: boolean = false
_currentIndex: number = 0
_errors: Error[] = ...
_initialRunnersCreated: boolean = false
_isIterableDone: boolean = false
_isRejected: boolean = false
_iterator: AsyncIterator<Element, any, undefined> | Iterator<Element, any, undefined>
_mapper: Mapper<Element, NewElement>
_options: Required<IterableMapperOptions>
_resolvingCount: number = 0
_unreadQueue: IterableQueue<NewElementOrError<NewElement>>

Methods

  • Returns AsyncIterator<NewElement, any, undefined>

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

    Returns Promise<IteratorResult<NewElement, any>>

    Iterator result

  • Get the next item from the source iterable.

    Returns Promise<void>

    Remarks

    This is called up to concurrency times in parallel.

    If the read queue is not full, and there are source items to read, each instance of this will keep calling a new instance of itself that detaches and runs asynchronously (keeping the same number of instances running).

    If the read queue + runners = max read queue length then the runner will exit and will be restarted when an item is read from the queue.

  • Throw an exception if the wrapped NewElement is an Error

    Parameters

    • item: NewElementOrError<NewElement>

    Returns NewElement

    Element if no error

Generated using TypeDoc