<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>
): interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<
(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.interface Pull<out A, out E = never, out Done = void, out R = never>An effectful pull step that either produces a value, fails with E, or
signals completion with Cause.Done<Done>.
When to use
Use to model one low-level pull step when a consumer repeatedly evaluates an
effect that may emit a value, fail normally, or signal normal completion
through Cause.Done.
Details
Pull represents completion in the error channel so low-level stream
consumers can distinguish ordinary failures from end-of-input and carry a
leftover value when needed.
Pull<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: (
input: Input
) => Pull.Pull<
Metadata<Output, Input>,
Error,
Output,
Env
>
step) => (input: Inputinput) => 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: (
input: Input
) => Pull.Pull<
Metadata<Output, Input>,
Error,
Output,
Env
>
step(input: Inputinput), (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.Metadata<Output, Input>.output: Outputoutput)
)