effect-io-ai

Package: effect
Module: Stream

Stream.groupedWithin

Partitions the stream into arrays, emitting when the chunk size is reached or the duration passes.

Example (Grouping elements by size or time)

import { Console, Effect, Stream } from "effect"

const program = Effect.gen(function*() {
  const values = yield* Stream.make(1, 2, 3).pipe(
    Stream.groupedWithin(2, "5 seconds"),
    Stream.runCollect
  )
  yield* Console.log(values)
})

Effect.runPromise(program)
// Output: [ [ 1, 2 ], [ 3 ] ]

Signature

declare const groupedWithin: { (chunkSize: number, duration: Duration.Input): <A, E, R>(self: Stream<A, E, R>) => Stream<Array<A>, E, R>; <A, E, R>(self: Stream<A, E, R>, chunkSize: number, duration: Duration.Input): Stream<Array<A>, E, R>; }

Source

Since v2.0.0