<Input, Output, EnvX, ErrorX, Error, Env>(
step: Effect<
(
options: InputMetadata<Input>
) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>,
Error,
Env
>
): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>Creates a Schedule from a step function that receives metadata about the schedule's execution.
Example (Creating a metadata-aware schedule)
import { Cause, Duration, Effect, Schedule } from "effect"
const firstThreeInputs = Schedule.fromStepWithMetadata(Effect.succeed((metadata: Schedule.InputMetadata<string>) => {
if (metadata.attempt > 3) {
return Cause.done("finished")
}
return Effect.succeed([
`attempt ${metadata.attempt}: ${metadata.input}`,
Duration.millis(250)
] as [string, Duration.Duration])
}))constructors
Source effect/Schedule.ts:33111 lines
export const const fromStepWithMetadata: <
Input,
Output,
EnvX,
ErrorX,
Error,
Env
>(
step: Effect<
(
options: InputMetadata<Input>
) => Pull.Pull<
[Output, Duration.Duration],
ErrorX,
Output,
EnvX
>,
Error,
Env
>
) => Schedule<
Output,
Input,
Error | Pull.ExcludeDone<ErrorX>,
Env | EnvX
>
Creates a Schedule from a step function that receives metadata about the schedule's execution.
Example (Creating a metadata-aware schedule)
import { Cause, Duration, Effect, Schedule } from "effect"
const firstThreeInputs = Schedule.fromStepWithMetadata(Effect.succeed((metadata: Schedule.InputMetadata<string>) => {
if (metadata.attempt > 3) {
return Cause.done("finished")
}
return Effect.succeed([
`attempt ${metadata.attempt}: ${metadata.input}`,
Duration.millis(250)
] as [string, Duration.Duration])
}))
fromStepWithMetadata = <function (type parameter) Input in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>Input, function (type parameter) Output in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>Output, function (type parameter) EnvX in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>EnvX, function (type parameter) ErrorX in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>ErrorX, function (type parameter) Error in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>Error, function (type parameter) Env in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>Env>(
step: Effect<
(
options: InputMetadata<Input>
) => Pull.Pull<
[Output, Duration.Duration],
ErrorX,
Output,
EnvX
>,
Error,
Env
>
(parameter) step: {
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; <…;
toString: () => string;
toJSON: () => unknown;
}
step: import EffectEffect<
(options: InputMetadata<Input>(parameter) options: {
input: Input;
attempt: number;
start: number;
now: number;
elapsed: number;
elapsedSincePrevious: number;
}
options: interface InputMetadata<Input>Metadata provided to schedule functions containing timing and input information.
InputMetadata<function (type parameter) Input in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>Input>) => import PullPull.type Pull.Pull = /*unresolved*/ anyPull<[function (type parameter) Output in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>Output, import DurationDuration.type Duration.Duration = /*unresolved*/ anyDuration], function (type parameter) ErrorX in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>ErrorX, function (type parameter) Output in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>Output, function (type parameter) EnvX in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>EnvX>,
function (type parameter) Error in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>Error,
function (type parameter) Env in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>Env
>
): 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 <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>Output, function (type parameter) Input in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>Input, function (type parameter) Error in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>Error | import PullPull.type Pull.ExcludeDone = /*unresolved*/ anyExcludeDone<function (type parameter) ErrorX in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>ErrorX>, function (type parameter) Env in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>Env | function (type parameter) EnvX in <Input, Output, EnvX, ErrorX, Error, Env>(step: Effect<(options: InputMetadata<Input>) => Pull.Pull<[Output, Duration.Duration], ErrorX, Output, EnvX>, Error, Env>): Schedule<Output, Input, Error | Pull.ExcludeDone<ErrorX>, Env | EnvX>EnvX> =>
const fromStep: <
Input,
Output,
EnvX,
Error,
ErrorX,
Env
>(
step: Effect<
(
now: number,
input: Input
) => Pull.Pull<
[Output, Duration.Duration],
ErrorX,
Output,
EnvX
>,
Error,
Env
>
) => Schedule<
Output,
Input,
Error | Pull.ExcludeDone<ErrorX>,
Env | EnvX
>
Creates a Schedule from a step function that returns a Pull.
Example (Creating a custom schedule from a step function)
import { Cause, Duration, Effect, Schedule } from "effect"
const schedule = Schedule.fromStep(Effect.sync(() => {
let count = 0
return (_now: number, _input: string) => {
if (count >= 3) {
return Cause.done(count)
}
return Effect.succeed([count++, Duration.millis(100)] as [number, Duration.Duration])
}
}))
fromStep(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: Effect<
(
options: InputMetadata<Input>
) => Pull.Pull<
[Output, Duration.Duration],
ErrorX,
Output,
EnvX
>,
Error,
Env
>
(parameter) step: {
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; <…;
toString: () => string;
toJSON: () => unknown;
}
step, (f: (
options: InputMetadata<Input>
) => Pull.Pull<
[Output, Duration.Duration],
ErrorX,
Output,
EnvX
>
f) => {
const const meta: <In>(
now: number,
input: In
) => InputMetadata<In>
meta = const metadataFn: () => <In>(
now: number,
input: In
) => InputMetadata<In>
metadataFn()
return (now: anynow, input: anyinput) => f: (
options: InputMetadata<Input>
) => Pull.Pull<
[Output, Duration.Duration],
ErrorX,
Output,
EnvX
>
f(const meta: <In>(
now: number,
input: In
) => InputMetadata<In>
meta(now: anynow, input: anyinput))
}))Referenced by 6 symbols