Hyperlinkv0.8.0-beta.28

Schedule

Schedule.mapconsteffect/Schedule.ts:1530
<Input, Output, Output2, Error2 = never, Env2 = never>(
  f: (
    metadata: Metadata<Output, Input>
  ) => Output2 | Effect<Output2, Error2, Env2>
): <Error, Env>(
  self: Schedule<Output, Input, Error, Env>
) => Schedule<Output2, Input, Error | Error2, Env | Env2>
<Output, Input, Error, Env, Output2, Error2 = never, Env2 = never>(
  self: Schedule<Output, Input, Error, Env>,
  f: (
    metadata: Metadata<Output, Input>
  ) => Output2 | Effect<Output2, Error2, Env2>
): Schedule<Output2, Input, Error | Error2, Env | Env2>

Returns a new Schedule that maps each schedule decision to a new output using the full schedule metadata.

Details

The callback receives the schedule input, output, selected delay duration, current attempt, and elapsed timing information. Return either a plain value or an Effect that produces the new output.

Example (Mapping schedule outputs)

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

// Transform schedule output from number to string
const countSchedule = Schedule.recurs(5).pipe(
  Schedule.map(({ output: count }) => Effect.succeed(`Execution #${count + 1}`))
)

// Map schedule delays to human-readable format
const readableDelays = Schedule.exponential("100 millis").pipe(
  Schedule.map(({ output: delay }) => Effect.succeed(`Next retry in ${delay}`))
)

// Transform numeric output to structured data
const structuredSchedule = Schedule.spaced("1 second").pipe(
  Schedule.map(({ output: recurrence }) => Effect.succeed({
    iteration: recurrence + 1,
    phase: recurrence < 5 ? "warmup" as const : "steady" as const
  }))
)

const program = Effect.gen(function*() {
  const results = yield* Effect.repeat(
    Effect.succeed("task completed"),
    structuredSchedule.pipe(
      Schedule.upTo({ times: 8 }),
      Schedule.tap(({ output: info }) =>
        Console.log(
          `${info.phase} phase - iteration ${info.iteration}`
        )
      )
    )
  )

  yield* Console.log(`Completed iterations`)
})

// Map with effectful transformation
const effectfulMap = Schedule.fixed("2 seconds").pipe(
  Schedule.map(({ output: count }) =>
    Effect.gen(function*() {
      yield* Console.log(`Processing count: ${count}`)
      return count * 10
    })
  )
)

// Use timing metadata in the mapped output
const complexSchedule = Schedule.fibonacci("100 millis").pipe(
  Schedule.map(({ output: delay, attempt }) =>
    Effect.succeed(`Attempt ${attempt} delay: ${delay}`)
  )
)
mapping
export const map: {
  <Input, Output, Output2, Error2 = never, Env2 = never>(
    f: (metadata: Metadata<Output, Input>) => Output2 | Effect<Output2, Error2, Env2>
  ): <Error, Env>(
    self: Schedule<Output, Input, Error, Env>
  ) => Schedule<Output2, Input, Error | Error2, Env | Env2>
  <Output, Input, Error, Env, Output2, Error2 = never, Env2 = never>(
    self: Schedule<Output, Input, Error, Env>,
    f: (metadata: Metadata<Output, Input>) => Output2 | Effect<Output2, Error2, Env2>
  ): Schedule<Output2, Input, Error | Error2, Env | Env2>
} = dual(2, <Output, Input, Error, Env, Output2, Error2 = never, Env2 = never>(
  self: Schedule<Output, Input, Error, Env>,
  f: (metadata: Metadata<Output, Input>) => Output2 | Effect<Output2, Error2, Env2>
): Schedule<Output2, Input, Error | Error2, Env | Env2> =>
  fromStep(effect.map(toStep(self), (step) => {
    const meta = metadataFn()
    return (now, input) =>
      Pull.matchEffect(step(now, input), {
        onSuccess: ([output, duration]) => {
          const result = f({ ...meta(now, input), output, duration })
          if (!isEffect(result)) return effect.succeed([result, duration] as [Output2, Duration.Duration])
          return effect.map(result, (output) => [output, duration] as [Output2, Duration.Duration])
        },
        onFailure: effect.failCause<Error>,
        onDone: (output) => {
          const result = f({ ...meta(now, input), output, duration: Duration.zero })
          if (!isEffect(result)) return Cause.done(result as Output2)
          return effect.flatMap(result, Cause.done)
        }
      })
  })))
Referenced by 2 symbols