Hyperlinkv0.8.0-beta.28

Channel

Channel.orElseIfEmptyconsteffect/Channel.ts:2734
<OutDone, OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>(
  f: (
    leftover: Types.NoInfer<OutDone>
  ) => Channel<
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1
  >
): <OutElem, OutErr, InElem, InErr, InDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>
) => Channel<
  OutElem | OutElem1,
  OutErr1 | OutErr,
  OutDone | OutDone1,
  InElem & InElem1,
  InErr & InErr1,
  InDone & InDone1,
  Env1 | Env
>
<
  OutElem,
  OutErr,
  OutDone,
  InElem,
  InErr,
  InDone,
  Env,
  OutElem1,
  OutErr1,
  OutDone1,
  InElem1,
  InErr1,
  InDone1,
  Env1
>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  f: (
    leftover: Types.NoInfer<OutDone>
  ) => Channel<
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1
  >
): Channel<
  OutElem | OutElem1,
  OutErr1 | OutErr,
  OutDone | OutDone1,
  InElem & InElem1,
  InErr & InErr1,
  InDone & InDone1,
  Env1 | Env
>

Runs a fallback channel if this channel completes without emitting any output elements.

Details

If the source emits at least one element, the source is used unchanged. If the source completes before emitting an element, the fallback function receives the source done value and returns the replacement channel.

sequencing
Source effect/Channel.ts:273494 lines
export const orElseIfEmpty: {
  <OutDone, OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>(
    f: (leftover: Types.NoInfer<OutDone>) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>
  ): <OutElem, OutErr, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>
  ) => Channel<
    OutElem | OutElem1,
    OutErr1 | OutErr,
    OutDone | OutDone1,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env1 | Env
  >
  <
    OutElem,
    OutErr,
    OutDone,
    InElem,
    InErr,
    InDone,
    Env,
    OutElem1,
    OutErr1,
    OutDone1,
    InElem1,
    InErr1,
    InDone1,
    Env1
  >(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    f: (leftover: Types.NoInfer<OutDone>) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>
  ): Channel<
    OutElem | OutElem1,
    OutErr1 | OutErr,
    OutDone | OutDone1,
    InElem & InElem1,
    InErr & InErr1,
    InDone & InDone1,
    Env1 | Env
  >
} = dual(2, <
  OutElem,
  OutErr,
  OutDone,
  InElem,
  InErr,
  InDone,
  Env,
  OutElem1,
  OutErr1,
  OutDone1,
  InElem1,
  InErr1,
  InDone1,
  Env1
>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  f: (leftover: Types.NoInfer<OutDone>) => Channel<OutElem1, OutErr1, OutDone1, InElem1, InErr1, InDone1, Env1>
): Channel<
  OutElem | OutElem1,
  OutErr1 | OutErr,
  OutDone | OutDone1,
  InElem & InElem1,
  InErr & InErr1,
  InDone & InDone1,
  Env1 | Env
> =>
  fromTransform((upstream, scope) =>
    Effect.sync(() => {
      let currentPull: Pull.Pull<OutElem | OutElem1, OutErr1 | OutErr, OutDone | OutDone1, Env1 | Env> | undefined
      const forkedScope = Scope.forkUnsafe(scope)
      const makePull = Effect.flatMap(toTransform(self)(upstream, forkedScope), (pull) => {
        const next = pull.pipe(
          Effect.tap(() => {
            currentPull = pull
            return Effect.void
          }),
          Pull.catchDone((leftover) =>
            Scope.close(forkedScope, Exit.succeed(leftover)).pipe(
              Effect.andThen(toTransform(f(leftover as OutDone))(upstream, scope)),
              Effect.flatMap((pull) => {
                currentPull = pull
                return pull
              })
            )
          )
        )
        currentPull = next
        return next
      })
      return Effect.suspend(() => currentPull ?? makePull)
    })
  ))
Referenced by 1 symbols