<Output, Input, Error, Env>(
schedule: Schedule<Output, Input, Error, Env>
): Effect<
(input: Input) => Pull.Pull<Output, Error, Output, Env>,
never,
Env
>Extracts a step function from a Schedule that automatically handles sleep delays.
Example (Extracting a sleeping step function)
import { Effect, Schedule } from "effect"
// Convert schedule to step function with automatic sleeping
const schedule = Schedule.spaced("1 second").pipe(Schedule.upTo({ times: 3 }))
const program = Effect.gen(function*() {
const stepWithSleep = yield* Schedule.toStepWithSleep(schedule)
// Each call will automatically sleep for the scheduled delay
console.log("Starting...")
const result1 = yield* stepWithSleep("first")
console.log(`First result: ${result1}`)
const result2 = yield* stepWithSleep("second")
console.log(`Second result: ${result2}`)
const result3 = yield* stepWithSleep("third")
console.log(`Third result: ${result3}`)
})export const const toStepWithSleep: <
Output,
Input,
Error,
Env
>(
schedule: Schedule<Output, Input, Error, Env>
) => Effect<
(
input: Input
) => Pull.Pull<Output, Error, Output, Env>,
never,
Env
>
Extracts a step function from a Schedule that automatically handles sleep delays.
Example (Extracting a sleeping step function)
import { Effect, Schedule } from "effect"
// Convert schedule to step function with automatic sleeping
const schedule = Schedule.spaced("1 second").pipe(Schedule.upTo({ times: 3 }))
const program = Effect.gen(function*() {
const stepWithSleep = yield* Schedule.toStepWithSleep(schedule)
// Each call will automatically sleep for the scheduled delay
console.log("Starting...")
const result1 = yield* stepWithSleep("first")
console.log(`First result: ${result1}`)
const result2 = yield* stepWithSleep("second")
console.log(`Second result: ${result2}`)
const result3 = yield* stepWithSleep("third")
console.log(`Third result: ${result3}`)
})
toStepWithSleep = <function (type parameter) Output in <Output, Input, Error, Env>(schedule: Schedule<Output, Input, Error, Env>): Effect<(input: Input) => Pull.Pull<Output, Error, Output, Env>, never, Env>Output, function (type parameter) Input in <Output, Input, Error, Env>(schedule: Schedule<Output, Input, Error, Env>): Effect<(input: Input) => Pull.Pull<Output, Error, Output, Env>, never, Env>Input, function (type parameter) Error in <Output, Input, Error, Env>(schedule: Schedule<Output, Input, Error, Env>): Effect<(input: Input) => Pull.Pull<Output, Error, Output, Env>, never, Env>Error, function (type parameter) Env in <Output, Input, Error, Env>(schedule: Schedule<Output, Input, Error, Env>): Effect<(input: Input) => Pull.Pull<Output, Error, Output, Env>, never, Env>Env>(
schedule: Schedule<Output, Input, Error, Env>(parameter) schedule: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
schedule: interface Schedule<out Output, in Input = unknown, out Error = never, out Env = never>A Schedule defines a strategy for repeating or retrying effects based on some policy.
Example (Defining retry and repeat schedules)
import { Console, Data, Effect, Schedule } from "effect"
class NetworkError extends Data.TaggedError("NetworkError")<{
readonly attempt: number
}> {}
// Basic retry schedule - retry up to 3 times with exponential backoff
const retrySchedule = Schedule.max([
Schedule.exponential("100 millis"),
Schedule.recurs(3)
])
// Basic repeat schedule - repeat every 30 seconds forever
const repeatSchedule: Schedule.Schedule<number, unknown, never> = Schedule
.spaced("30 seconds")
const program = Effect.gen(function*() {
let attempts = 0
const result1 = yield* Effect.retry(
Effect.gen(function*() {
attempts++
if (attempts < 3) {
return yield* Effect.fail(new NetworkError({ attempt: attempts }))
}
return "Success"
}),
retrySchedule
)
console.log(result1) // "Success"
yield* Console.log("heartbeat").pipe(
Effect.repeat(repeatSchedule.pipe(Schedule.upTo({ times: 5 })))
)
})
The Schedule namespace contains types and utilities for working with schedules.
Schedule<function (type parameter) Output in <Output, Input, Error, Env>(schedule: Schedule<Output, Input, Error, Env>): Effect<(input: Input) => Pull.Pull<Output, Error, Output, Env>, never, Env>Output, function (type parameter) Input in <Output, Input, Error, Env>(schedule: Schedule<Output, Input, Error, Env>): Effect<(input: Input) => Pull.Pull<Output, Error, Output, Env>, never, Env>Input, function (type parameter) Error in <Output, Input, Error, Env>(schedule: Schedule<Output, Input, Error, Env>): Effect<(input: Input) => Pull.Pull<Output, Error, Output, Env>, never, Env>Error, function (type parameter) Env in <Output, Input, Error, Env>(schedule: Schedule<Output, Input, Error, Env>): Effect<(input: Input) => Pull.Pull<Output, Error, Output, Env>, never, Env>Env>
): import EffectEffect<
(input: Inputinput: function (type parameter) Input in <Output, Input, Error, Env>(schedule: Schedule<Output, Input, Error, Env>): Effect<(input: Input) => Pull.Pull<Output, Error, Output, Env>, never, Env>Input) => import PullPull.type Pull.Pull = /*unresolved*/ anyPull<function (type parameter) Output in <Output, Input, Error, Env>(schedule: Schedule<Output, Input, Error, Env>): Effect<(input: Input) => Pull.Pull<Output, Error, Output, Env>, never, Env>Output, function (type parameter) Error in <Output, Input, Error, Env>(schedule: Schedule<Output, Input, Error, Env>): Effect<(input: Input) => Pull.Pull<Output, Error, Output, Env>, never, Env>Error, function (type parameter) Output in <Output, Input, Error, Env>(schedule: Schedule<Output, Input, Error, Env>): Effect<(input: Input) => Pull.Pull<Output, Error, Output, Env>, never, Env>Output, function (type parameter) Env in <Output, Input, Error, Env>(schedule: Schedule<Output, Input, Error, Env>): Effect<(input: Input) => Pull.Pull<Output, Error, Output, Env>, never, Env>Env>,
never,
function (type parameter) Env in <Output, Input, Error, Env>(schedule: Schedule<Output, Input, Error, Env>): Effect<(input: Input) => Pull.Pull<Output, Error, Output, Env>, never, Env>Env
> =>
import effecteffect.const map: {
<A, B>(f: (a: A) => B): <E, R>(
self: Effect.Effect<A, E, R>
) => Effect.Effect<B, E, R>
<A, E, R, B>(
self: Effect.Effect<A, E, R>,
f: (a: A) => B
): Effect.Effect<B, E, R>
}
map(
const toStepWithMetadata: <
Output,
Input,
Error,
Env
>(
schedule: Schedule<Output, Input, Error, Env>
) => Effect<
(
input: Input
) => Pull.Pull<
Metadata<Output, Input>,
Error,
Output,
Env
>,
never,
Env
>
Extracts a step function from a Schedule that sleeps for each computed
delay and returns metadata for the completed step.
When to use
Use to drive a schedule manually while preserving the computed output,
delay, input, attempt, and elapsed timing metadata for each step.
Details
The returned step reads the current time from Clock when invoked, calls the
schedule step with that timestamp and input, sleeps for the returned
duration, and then yields Metadata.
toStepWithMetadata(schedule: Schedule<Output, Input, Error, Env>(parameter) schedule: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
schedule),
(step: unknownstep) => (input: anyinput) => import effecteffect.const map: {
<A, B>(f: (a: A) => B): <E, R>(
self: Effect.Effect<A, E, R>
) => Effect.Effect<B, E, R>
<A, E, R, B>(
self: Effect.Effect<A, E, R>,
f: (a: A) => B
): Effect.Effect<B, E, R>
}
map(step: unknownstep(input: anyinput), (meta: Metadata<Output, Input>(parameter) meta: {
output: Output;
duration: Duration.Duration;
input: Input;
attempt: number;
start: number;
now: number;
elapsed: number;
elapsedSincePrevious: number;
}
meta) => meta: Metadata<Output, Input>(parameter) meta: {
output: Output;
duration: Duration.Duration;
input: Input;
attempt: number;
start: number;
now: number;
elapsed: number;
elapsedSincePrevious: number;
}
meta.output)
)