import { SinkInternal } from "@effect/core/stream/Sink/operations/_internal/SinkInternal" /** * A sink that folds its input chunks with the provided function, termination * predicate and initial state. The `cont` condition is checked only for the * initial value and at the end of processing of each chunk. `f` and `cont` * must preserve chunking-invariance. * * @tsplus static effect/core/stream/Sink.Ops foldChunksEffect */ export function foldChunksEffect( z: S, cont: Predicate, f: (s: S, input: Chunk) => Effect ): Sink { return Sink.suspend(new SinkInternal(reader(z, cont, f))) } function reader( z: S, cont: Predicate, f: (s: S, input: Chunk) => Effect ): Channel, unknown, E, never, S> { return cont(z) ? Channel.readWith( (chunk: Chunk) => Channel.fromEffect(f(z, chunk)).flatMap((nextS) => reader(nextS, cont, f)), (err) => Channel.fail(err), () => Channel.succeed(z) ) : Channel.succeed(z) }