Hyperlinkv0.8.0-beta.28

Schedule

Schedule.andThenResultconsteffect/Schedule.ts:689
<Output2, Input2, Error2, Env2>(
  other: Schedule<Output2, Input2, Error2, Env2>
): <Output, Input, Error, Env>(
  self: Schedule<Output, Input, Error, Env>
) => Schedule<
  Result.Result<Output2, Output>,
  Input & Input2,
  Error | Error2,
  Env | Env2
>
<Output, Input, Error, Env, Output2, Input2, Error2, Env2>(
  self: Schedule<Output, Input, Error, Env>,
  other: Schedule<Output2, Input2, Error2, Env2>
): Schedule<
  Result.Result<Output2, Output>,
  Input & Input2,
  Error | Error2,
  Env | Env2
>

Returns a schedule that runs self to completion, then runs other, and preserves which schedule produced each output.

Details

The resulting schedule emits a Result to indicate which phase produced each output: outputs from self are emitted as Failure, and outputs from other are emitted as Success.

Example (Tracking sequential schedule phases)

import { Console, Effect, Result, Schedule } from "effect"

// Track which phase of the schedule we're in
const phaseTracker = Schedule.andThenResult(
  Schedule.exponential("100 millis").pipe(Schedule.upTo({ times: 2 })),
  Schedule.spaced("500 millis").pipe(Schedule.upTo({ times: 2 }))
)

const program = Effect.gen(function*() {
  yield* Effect.repeat(
    Effect.gen(function*() {
      yield* Console.log("Task executed")
      return "task-result"
    }),
    phaseTracker.pipe(
      Schedule.tap(({ output: result }) =>
        Result.match(result, {
          onFailure: (phase1Output) => Console.log(`Phase 1: ${phase1Output}`),
          onSuccess: (phase2Output) => Console.log(`Phase 2: ${phase2Output}`)
        })
      )
    )
  )
})
sequencing
Source effect/Schedule.ts:68958 lines
export const andThenResult: {
  <Output2, Input2, Error2, Env2>(
    other: Schedule<Output2, Input2, Error2, Env2>
  ): <Output, Input, Error, Env>(
    self: Schedule<Output, Input, Error, Env>
  ) => Schedule<Result.Result<Output2, Output>, Input & Input2, Error | Error2, Env | Env2>
  <Output, Input, Error, Env, Output2, Input2, Error2, Env2>(
    self: Schedule<Output, Input, Error, Env>,
    other: Schedule<Output2, Input2, Error2, Env2>
  ): Schedule<Result.Result<Output2, Output>, Input & Input2, Error | Error2, Env | Env2>
} = dual(2, <Output, Input, Error, Env, Output2, Input2, Error2, Env2>(
  self: Schedule<Output, Input, Error, Env>,
  other: Schedule<Output2, Input2, Error2, Env2>
): Schedule<Result.Result<Output2, Output>, Input & Input2, Error | Error2, Env | Env2> =>
  fromStep(effect.sync(() => {
    let currentSide = 0
    let currentStep:
      | undefined
      | ((now: number, input: Input & Input2) => Pull.Pull<
        [Result.Result<Output2, Output>, Duration.Duration],
        Error | Error2,
        Result.Result<Output2, Output>,
        Env | Env2
      >)
    const left = map(self, ({ output }) => Result.fail(output))
    const right = map(other, ({ output }) => Result.succeed(output))
    return function recur(
      now,
      input
    ): Pull.Pull<
      [Result.Result<Output2, Output>, Duration.Duration],
      Error | Error2,
      Result.Result<Output2, Output>,
      Env | Env2
    > {
      if (currentStep) return currentStep(now, input)
      return toStep<
        Result.Result<Output2, Output>,
        Input & Input2,
        Error | Error2,
        Env | Env2
      >(currentSide === 0 ? left : right).pipe(
        effect.flatMap((step) => {
          currentSide++
          if (currentSide === 1) {
            currentStep = (now, input) =>
              Pull.catchDone(step(now, input), (_) => {
                currentStep = undefined
                return recur(now, input)
              })
            return currentStep(now, input)
          }
          currentStep = step
          return currentStep(now, input)
        })
      )
    }
  })))
Referenced by 1 symbols