// ets_tracing: off import type * as CK from "../../../../Collections/Immutable/Chunk/index.js" import * as T from "../../../../Effect/index.js" import * as O from "../../../../Option/index.js" import * as P from "../../../../Promise/index.js" import * as CH from "../../Channel/index.js" import * as C from "../core.js" /** * Halts the evaluation of this stream when the provided promise resolves. * * If the promise completes with a failure, the stream will emit that failure. */ export function haltWhenP_( self: C.Stream, p: P.Promise ): C.Stream { const writer = (): CH.Channel< R, E | E1, CK.Chunk, unknown, E | E1, CK.Chunk, void > => CH.unwrap( T.map_( P.poll(p), O.fold( () => CH.readWith( (in_) => CH.zipRight_(CH.write(in_), writer()), (err) => CH.fail(err), (_) => CH.unit ), (io) => CH.unwrap( T.fold_( io, (_) => CH.fail(_), (_) => CH.unit ) ) ) ) ) return new C.Stream(self.channel[">>>"](writer())) } /** * Halts the evaluation of this stream when the provided promise resolves. * * If the promise completes with a failure, the stream will emit that failure. * * @ets_data_first haltWhenP_ */ export function haltWhenP(p: P.Promise) { return (self: C.Stream) => haltWhenP_(self, p) }