// ets_tracing: off import "../../../Operator/index.js" import * as Cause from "../../../Cause/index.js" import type * as T from "../../../Effect/index.js" import type * as Exit from "../../../Exit/index.js" import { identity } from "../../../Function/index.js" import * as P from "./_internal/primitives.js" import type * as PR from "./_internal/producer.js" export type { SingleProducerAsyncInput, AsyncInputProducer, AsyncInputConsumer } from "./_internal/producer.js" export { makeSingleProducerAsyncInput } from "./_internal/producer.js" export * from "./_internal/primitives.js" /** * Pipe the output of a channel into the input of another */ export function pipeTo_< Env, Env2, InErr, InElem, InDone, OutErr, OutElem, OutDone, OutErr2, OutElem2, OutDone2 >( left: P.Channel, right: P.Channel ): P.Channel { return new P.PipeTo< Env & Env2, InErr, InElem, InDone, OutErr2, OutElem2, OutDone2, OutErr, OutElem, OutDone >( () => left, () => right ) } /** * Pipe the output of a channel into the input of another * * @ets_data_first pipeTo_ */ export function pipeTo( right: P.Channel ): ( left: P.Channel ) => P.Channel { return (left) => pipeTo_(left, right) } /** * Reads an input and continue exposing both full error cause and completion */ export function readWithCause< Env, Env1, Env2, InErr, InElem, InDone, OutErr, OutErr1, OutErr2, OutElem, OutElem1, OutElem2, OutDone, OutDone1, OutDone2 >( inp: (i: InElem) => P.Channel, halt: ( e: Cause.Cause ) => P.Channel, done: ( d: InDone ) => P.Channel ): P.Channel< Env & Env1 & Env2, InErr, InElem, InDone, OutErr | OutErr1 | OutErr2, OutElem | OutElem1 | OutElem2, OutDone | OutDone1 | OutDone2 > { return new P.Read< Env & Env1 & Env2, InErr, InElem, InDone, OutErr | OutErr1 | OutErr2, OutElem | OutElem1 | OutElem2, OutDone | OutDone1 | OutDone2, InErr, InDone >( inp, new P.ContinuationK< Env & Env1 & Env2, InErr, InElem, InDone, InErr, OutErr | OutErr1 | OutErr2, OutElem | OutElem1 | OutElem2, InDone, OutDone | OutDone1 | OutDone2 >(done, halt) ) } /** * End a channel with the specified result */ export function endWith( result: () => OutDone ): P.Channel { return new P.Done(result) } /** * End a channel with the specified result */ export function end( result: OutDone ): P.Channel { return new P.Done(() => result) } /** * Halt a channel with the specified cause */ export function failCauseWith( result: () => Cause.Cause ): P.Channel { return new P.Halt(result) } /** * Halt a channel with the specified cause */ export function failCause( result: Cause.Cause ): P.Channel { return new P.Halt(() => result) } /** * Halt a channel with the specified error */ export function failWith( error: () => E ): P.Channel { return new P.Halt(() => Cause.fail(error())) } /** * Halt a channel with the specified error */ export function fail( error: E ): P.Channel { return new P.Halt(() => Cause.fail(error)) } /** * Halt a channel with the specified exception */ export function die( defect: unknown ): P.Channel { return new P.Halt(() => Cause.die(defect)) } /** * Halt a channel with the specified exception */ export function dieWith( defect: () => unknown ): P.Channel { return new P.Halt(() => Cause.die(defect())) } /** * Writes an output to the channel */ export function writeWith( out: () => OutElem ): P.Channel { return new P.Emit(out) } /** * Writes an output to the channel */ export function write( out: OutElem ): P.Channel { return new P.Emit(() => out) } /** * Returns a new channel with an attached finalizer. The finalizer is guaranteed to be executed * so long as the channel begins execution (and regardless of whether or not it completes). */ export function ensuringWith_< Env, Env2, InErr, InElem, InDone, OutErr, OutElem, OutDone >( channel: P.Channel, finalizer: (e: Exit.Exit) => T.Effect ): P.Channel { return new P.Ensuring( channel, finalizer ) } /** * Returns a new channel with an attached finalizer. The finalizer is guaranteed to be executed * so long as the channel begins execution (and regardless of whether or not it completes). * * @ets_data_first ensuringWith_ */ export function ensuringWith( finalizer: (e: Exit.Exit) => T.Effect ): ( channel: P.Channel ) => P.Channel { return (channel) => ensuringWith_(channel, finalizer) } /** * Returns a new channel whose outputs are fed to the specified factory function, which creates * new channels in response. These new channels are sequentially concatenated together, and all * their outputs appear as outputs of the newly returned channel. The provided merging function * is used to merge the terminal values of all channels into the single terminal value of the * returned channel. */ export function concatMapWith_< Env, InErr, InElem, InDone, OutErr, OutElem, OutElem2, OutDone, OutDone2, OutDone3, Env2, InErr2, InElem2, InDone2, OutErr2 >( self: P.Channel, f: ( o: OutElem ) => P.Channel, g: (o: OutDone, o1: OutDone) => OutDone, h: (o: OutDone, o2: OutDone2) => OutDone3 ): P.Channel< Env & Env2, InErr & InErr2, InElem & InElem2, InDone & InDone2, OutErr | OutErr2, OutElem2, OutDone3 > { return new P.ConcatAll< Env & Env2, InErr & InErr2, InElem & InElem2, InDone & InDone2, OutErr | OutErr2, OutElem2, OutDone3, OutElem, OutDone, OutDone2 >(g, h, self, f) } /** * Returns a new channel whose outputs are fed to the specified factory function, which creates * new channels in response. These new channels are sequentially concatenated together, and all * their outputs appear as outputs of the newly returned channel. The provided merging function * is used to merge the terminal values of all channels into the single terminal value of the * returned channel. * * @ets_data_first concatMapWith_ */ export function concatMapWith< OutDone, OutElem, Env2, InErr2, InElem2, InDone2, OutErr2, OutElem2, OutDone2, OutDone3 >( f: ( o: OutElem ) => P.Channel, g: (o: OutDone, o1: OutDone) => OutDone, h: (o: OutDone, o2: OutDone2) => OutDone3 ): ( self: P.Channel ) => P.Channel< Env & Env2, InErr & InErr2, InElem & InElem2, InDone & InDone2, OutErr | OutErr2, OutElem2, OutDone3 > { return (self) => concatMapWith_(self, f, g, h) } /** * Concat sequentially a channel of channels */ export function concatAllWith_< Env, InErr, InElem, InDone, OutErr, OutElem, OutDone, OutDone2, OutDone3, Env2, InErr2, InElem2, InDone2, OutErr2 >( channels: P.Channel< Env, InErr, InElem, InDone, OutErr, P.Channel, OutDone2 >, f: (o: OutDone, o1: OutDone) => OutDone, g: (o: OutDone, o2: OutDone2) => OutDone3 ): P.Channel< Env & Env2, InErr & InErr2, InElem & InElem2, InDone & InDone2, OutErr | OutErr2, OutElem, OutDone3 > { return new P.ConcatAll< Env & Env2, InErr & InErr2, InElem & InElem2, InDone & InDone2, OutErr | OutErr2, OutElem, OutDone3, P.Channel, OutDone, OutDone2 >(f, g, channels, identity) } /** * Concat sequentially a channel of channels * * @ets_data_first concatAllWith_ */ export function concatAllWith( f: (o: OutDone, o1: OutDone) => OutDone, g: (o: OutDone, o2: OutDone2) => OutDone3 ): < Env, InErr, InElem, InDone, OutErr, OutElem, Env2, InErr2, InElem2, InDone2, OutErr2 >( channels: P.Channel< Env, InErr, InElem, InDone, OutErr, P.Channel, OutDone2 > ) => P.Channel< Env & Env2, InErr & InErr2, InElem & InElem2, InDone & InDone2, OutErr | OutErr2, OutElem, OutDone3 > { return (channels) => concatAllWith_(channels, f, g) } /** * Fold the channel exposing success and full error cause */ export function foldCauseChannel_< Env, Env1, Env2, InErr, InErr1, InErr2, InElem, InElem1, InElem2, InDone, InDone1, InDone2, OutErr, OutErr2, OutErr3, OutElem, OutElem1, OutElem2, OutDone, OutDone2, OutDone3 >( self: P.Channel, onErr: ( c: Cause.Cause ) => P.Channel, onSucc: ( o: OutDone ) => P.Channel ): P.Channel< Env & Env1 & Env2, InErr & InErr1 & InErr2, InElem & InElem1 & InElem2, InDone & InDone1 & InDone2, OutErr2 | OutErr3, OutElem | OutElem1 | OutElem2, OutDone2 | OutDone3 > { return new P.Fold< Env & Env1 & Env2, InErr & InErr1 & InErr2, InElem & InElem1 & InElem2, InDone & InDone1 & InDone2, OutErr2 | OutErr3, OutElem | OutElem1 | OutElem2, OutDone2 | OutDone3, OutErr, OutDone >( self, new P.ContinuationK< Env & Env1 & Env2, InErr & InErr1 & InErr2, InElem & InElem1 & InElem2, InDone & InDone1 & InDone2, OutErr, OutErr2 | OutErr3, OutElem | OutElem1 | OutElem2, OutDone, OutDone2 | OutDone3 >(onSucc, onErr) ) } /** * Fold the channel exposing success and full error cause * * @ets_data_first foldCauseChannel_ */ export function foldCauseChannel< Env1, Env2, InErr1, InErr2, InElem1, InElem2, InDone1, InDone2, OutErr, OutErr2, OutErr3, OutElem1, OutElem2, OutDone, OutDone2, OutDone3 >( onErr: ( c: Cause.Cause ) => P.Channel, onSucc: ( o: OutDone ) => P.Channel ): ( self: P.Channel ) => P.Channel< Env & Env1 & Env2, InErr & InErr1 & InErr2, InElem & InElem1 & InElem2, InDone & InDone1 & InDone2, OutErr2 | OutErr3, OutElem | OutElem1 | OutElem2, OutDone2 | OutDone3 > { return (self) => foldCauseChannel_(self, onErr, onSucc) } /** * Embed inputs from continuos pulling of a producer */ export function embedInput_( self: P.Channel, input: PR.AsyncInputProducer ): P.Channel { return new P.Bridge(input, self) } /** * Embed inputs from continuos pulling of a producer * * @ets_data_first embedInput_ */ export function embedInput( input: PR.AsyncInputProducer ): ( self: P.Channel ) => P.Channel { return (self) => embedInput_(self, input) } /** * Construct a resource Channel with Acquire / Release */ export function acquireReleaseOutExitWith_( self: T.Effect, release: (z: Z, e: Exit.Exit) => T.RIO ): P.Channel { return new P.BracketOut(self, release) } /** * Construct a resource Channel with Acquire / Release * * @ets_data_first acquireReleaseOutExitWith_ */ export function acquireReleaseOutExitWith( release: (z: Z, e: Exit.Exit) => T.RIO ): ( self: T.Effect ) => P.Channel { return (self) => acquireReleaseOutExitWith_(self, release) } /** * Provides the channel with its required environment, which eliminates * its dependency on `Env`. */ export function provideAll_( self: P.Channel, env: Env ): P.Channel { return new P.Provide(env, self) } /** * Provides the channel with its required environment, which eliminates * its dependency on `Env`. * * @ets_data_first provideAll_ */ export function provideAll( env: Env ): ( self: P.Channel ) => P.Channel { return (self) => provideAll_(self, env) } /** * Returns a new channel, which sequentially combines this channel, together with the provided * factory function, which creates a second channel based on the terminal value of this channel. * The result is a channel that will first perform the functions of this channel, before * performing the functions of the created channel (including yielding its terminal value). */ export function chain_< Env, InErr, InElem, InDone, OutErr, OutElem, OutDone, Env1, InErr1, InElem1, InDone1, OutErr1, OutElem1, OutDone2 >( self: P.Channel, f: ( d: OutDone ) => P.Channel ): P.Channel< Env & Env1, InErr & InErr1, InElem & InElem1, InDone & InDone1, OutErr | OutErr1, OutElem | OutElem1, OutDone2 > { return new P.Fold< Env & Env1, InErr & InErr1, InElem & InElem1, InDone & InDone1, OutErr | OutErr1, OutElem | OutElem1, OutDone2, OutErr | OutErr1, OutDone >(self, new P.ContinuationK(f, failCause)) } /** * Returns a new channel, which sequentially combines this channel, together with the provided * factory function, which creates a second channel based on the terminal value of this channel. * The result is a channel that will first perform the functions of this channel, before * performing the functions of the created channel (including yielding its terminal value). * * @ets_data_first chain_ */ export function chain< OutDone, Env1, InErr1, InElem1, InDone1, OutErr1, OutElem1, OutDone2 >( f: ( d: OutDone ) => P.Channel ): ( self: P.Channel ) => P.Channel< Env & Env1, InErr & InErr1, InElem & InElem1, InDone & InDone1, OutErr | OutErr1, OutElem | OutElem1, OutDone2 > { return (self) => chain_(self, f) } export function suspend( effect: () => P.Channel ): P.Channel { return new P.EffectSuspendTotal(effect) } /** * Use an effect to end a channel */ export function fromEffect( self: T.Effect ): P.Channel { return new P.Effect(self) } export function succeedWith( effect: () => OutDone ): P.Channel { return new P.EffectTotal(effect) } export function readOrFail( e: E ): P.Channel { return new P.Read( (in_) => end(in_), new P.ContinuationK( (_) => fail(e), (_) => fail(e) ) ) } /** * Returns a new channel that is the same as this one, except if this channel errors for any * typed error, then the returned channel will switch over to using the fallback channel returned * by the specified error handler. */ export function catchAllCause_< Env, Env1, InErr, InErr1, InElem, InElem1, InDone, InDone1, OutErr, OutErr1, OutElem, OutElem1, OutDone, OutDone1 >( self: P.Channel, f: ( cause: Cause.Cause ) => P.Channel ): P.Channel< Env & Env1, InErr & InErr1, InElem & InElem1, InDone & InDone1, OutErr1, OutElem | OutElem1, OutDone | OutDone1 > { return new P.Fold< Env & Env1, InErr & InErr1, InElem & InElem1, InDone & InDone1, OutErr1, OutElem | OutElem1, OutDone | OutDone1, OutErr, OutDone | OutDone1 >(self, new P.ContinuationK((_) => end(_), f)) } /** * Returns a new channel that is the same as this one, except if this channel errors for any * typed error, then the returned channel will switch over to using the fallback channel returned * by the specified error handler. * * @ets_data_first catchAllCause_ */ export function catchAllCause< Env1, InErr1, InElem1, InDone1, OutErr, OutErr1, OutElem1, OutDone1 >( f: ( cause: Cause.Cause ) => P.Channel ) { return ( self: P.Channel ) => catchAllCause_(self, f) }