import type { ChunkBuilder } from "@tsplus/stdlib/collections/Chunk" /** * Returns a new channel, which is the same as this one, except that all the * outputs are collected and bundled into a tuple together with the terminal * value of this channel. * * As the channel returned from this channel collects all of this channel's * output into an in- memory chunk, it is not safe to call this method on * channels that output a large or unbounded number of values. * * @tsplus getter effect/core/stream/Channel doneCollect */ export function doneCollect( self: Channel ): Channel< Env, InErr, InElem, InDone, OutErr, never, readonly [Chunk, OutDone] > { return Channel.suspend(() => { const builder = Chunk.builder() return (self >> reader(builder)).flatMap((z) => Channel.sync([builder.build(), z]) ) }) } function reader( builder: ChunkBuilder ): Channel { return Channel.readWith( (outElem) => Channel.sync(() => { builder.append(outElem) }).flatMap(() => reader(builder)), (outErr) => Channel.fail(outErr), (outDone) => Channel.succeed(outDone) ) }