@metreeca/flow - v0.12.0
    Preparing search index...

    Function chain

    • Creates a task emitting the items a mapper computes over the feed.

      The mapper is handed the feed and computes the items to carry on with, so a step deciding on the feed as a whole, reconciling it against a stored snapshot, ranking it or clearing it against a quota before letting anything through, is written as an ordinary asynchronous function of the feed rather than as a generator of its own. Sinks already available are lifted back into the pipe the same way: handing over toSet carries on with the distinct items alone.

      The items are supplied either as a batch listing them or as a feed yielding them, handed back as they are or awaited, so a step composing the feed it draws from with tasks already available is spared the generator as well.

      Whatever the mapper computes is emitted item by item, so the items carried on with need be neither the ones drawn, nor as many, nor of the same type: a mapper computing nothing empties the feed, and one computing items of its own emits them even where the feed drew none.

      Warning

      • Exhaustive: nothing is emitted before the mapper resolves, so a mapper drawing the feed entire, as most do, never completes on an infinite feed; one composing the feed it draws from emits as its items are drawn instead.
      • Materialising: a batch is held whole before its first item is emitted, on top of whatever the mapper retains while computing it, while a feed is drawn item by item.
      • Stateful: the outcome covers the items drawn, so a task invoked per nested feed or per run decides on each independently rather than on the feed as a whole.
      Note

      A mapper computing a string emits its characters, as any other iterable does its items: carry a string on whole by wrapping it in an array.

      Type Parameters

      • V

        The type of items drawn from the feed

      • R

        The type of items the mapper computes

      Parameters

      • mapper: (feed: Feed<NoInfer<V>>) => Awaitable<Awaitables<R>>

        The function drawing the feed and computing the items to carry on with

      Returns Task<V, R>

      A task yielding the items mapper computes over the feed

      await pipe(
      (items([1, 2, 3, 4]))
      (chain(async feed => (await feed(toArray())).slice(-2)))
      (toArray())
      ); // [3, 4], as the last items are known only once the feed runs dry

      await pipe(
      (items([1, 2, 2, 3]))
      (chain(toSet()))
      (toArray())
      ); // [1, 2, 3], as a sink already available is lifted back into the pipe