@azerum/ts-csp
    Preparing search index...

    Function partitionTime

    • Reads from source channel in groups of size groupSize. However, if more than nextValueTimeoutMs elapse since the last read from source, yields an incomplete group (with length < groupSize) early

      The returned iterable is closed once source closes. If there is an incomplete group, it is yielded before closing

      Never yields empty arrays

      Type Parameters

      Parameters

      • source: ReadableChannel<T>
      • groupSize: number

        Must be an integer >= 1

      • nextValueTimeoutMs: number

      Returns AsyncIterable<[T, ...T[]]>

      Simple example:

      function numbers(): ReadableChannel<number> {
      const ch = new Channel<number>(0)

      void (async () => {
      for (let i = 0;; ++i) {
      await ch.write(i)
      }
      })()

      return ch
      }

      // This gives iterable:
      // [0, 1, 2]
      // [3, 4, 5]
      // [6, 7, 8]
      // ...
      const groups = partitionTime(numbers(), 3, 1000)

      Grouping with timeout:

      function producer(): ReadableChannel<Something> {
      // ...
      }

      async function saver(values: ReadableChannel<Something>) {
      const batchedValues = partitionTime(values, 50, 10_000)

      // Does not wait for longer than 10s if `producer()` produces
      // less than 50 values
      for await (const batch of batchedValues)) {
      await insertInDb(batch)
      }
      }