<A, E, R, Out, E2, R2>(
acquire: Effect.Effect<A, E, R>,
policy: Schedule.Schedule<Out, unknown, E2, R2>
): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>Creates a Resource that refreshes automatically according to the supplied
schedule.
When to use
Use when a resource should refresh in the background according to a schedule for the lifetime of its scope.
export const const auto: <A, E, R, Out, E2, R2>(
acquire: Effect.Effect<A, E, R>,
policy: Schedule.Schedule<Out, unknown, E2, R2>
) => Effect.Effect<
Resource<A, E>,
never,
R | R2 | Scope.Scope
>
Creates a Resource that refreshes automatically according to the supplied
schedule.
When to use
Use when a resource should refresh in the background according to a schedule
for the lifetime of its scope.
auto = <function (type parameter) A in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>A, function (type parameter) E in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>E, function (type parameter) R in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>R, function (type parameter) Out in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>Out, function (type parameter) E2 in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>E2, function (type parameter) R2 in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>R2>(
acquire: Effect.Effect<A, E, R>(parameter) acquire: {
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;
}
acquire: import EffectEffect.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<function (type parameter) A in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>A, function (type parameter) E in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>E, function (type parameter) R in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>R>,
policy: Schedule.Schedule<Out, unknown, E2, R2>(parameter) policy: {
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; <…;
}
policy: import ScheduleSchedule.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) Out in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>Out, unknown, function (type parameter) E2 in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>E2, function (type parameter) R2 in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>R2>
): import EffectEffect.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<interface Resource<in out A, in out E = never>A Resource is a value loaded into memory that can be refreshed manually or
automatically according to a schedule.
When to use
Use to model a scoped value whose latest acquisition result is kept available
for repeated reads and can be refreshed manually or on a schedule.
Resource<function (type parameter) A in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>A, function (type parameter) E in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>E>, never, function (type parameter) R in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>R | function (type parameter) R2 in <A, E, R, Out, E2, R2>(acquire: Effect.Effect<A, E, R>, policy: Schedule.Schedule<Out, unknown, E2, R2>): Effect.Effect<Resource<A, E>, never, R | R2 | Scope.Scope>R2 | import ScopeScope.Scope> =>
import EffectEffect.const tap: {
<A, B, E2, R2>(
f: (a: NoInfer<A>) => Effect<B, E2, R2>
): <E, R>(
self: Effect<A, E, R>
) => Effect<A, E | E2, R | R2>
<B, E2, R2>(f: Effect<B, E2, R2>): <A, E, R>(
self: Effect<A, E, R>
) => Effect<A, E | E2, R | R2>
<A, E, R, B, E2, R2>(
self: Effect<A, E, R>,
f: (a: NoInfer<A>) => Effect<B, E2, R2>
): Effect<A, E | E2, R | R2>
<A, E, R, B, E2, R2>(
self: Effect<A, E, R>,
f: Effect<B, E2, R2>
): Effect<A, E | E2, R | R2>
}
tap(
const manual: <A, E, R>(
acquire: Effect.Effect<A, E, R>
) => Effect.Effect<
Resource<A, E>,
never,
Scope.Scope | R
>
Creates a Resource that must be refreshed manually.
When to use
Use when you need manual control over resource refresh timing rather than an
automatic schedule.
manual(acquire: Effect.Effect<A, E, R>(parameter) acquire: {
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;
}
acquire),
(self: Resource<A, E>(parameter) self: {
scopedRef: ScopedRef.ScopedRef<Exit.Exit<A, E>>;
acquire: Effect.Effect<A, E>;
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; <…;
}
self) => import EffectEffect.const forkScoped: <
Arg extends
| Effect<any, any, any>
| {
readonly startImmediately?:
| boolean
| undefined
readonly uninterruptible?:
| boolean
| "inherit"
| undefined
}
| undefined = {
readonly startImmediately?:
| boolean
| undefined
readonly uninterruptible?:
| boolean
| "inherit"
| undefined
}
>(
effectOrOptions?: Arg,
options?:
| {
readonly startImmediately?:
| boolean
| undefined
readonly uninterruptible?:
| boolean
| "inherit"
| undefined
}
| undefined
) => [Arg] extends [
Effect<infer _A, infer _E, infer _R>
]
? Effect<Fiber<_A, _E>, never, _R | Scope>
: <A, E, R>(
self: Effect<A, E, R>
) => Effect<Fiber<A, E>, never, R | Scope>
Forks the fiber in a Scope, interrupting it when the scope is closed.
Example (Forking into the current scope)
import { Effect } from "effect"
const backgroundTask = Effect.gen(function*() {
yield* Effect.sleep("5 seconds")
yield* Effect.log("Background task completed")
return "result"
})
const program = Effect.scoped(
Effect.gen(function*() {
const fiber = yield* backgroundTask.pipe(Effect.forkScoped)
// or fork a fiber that starts immediately:
yield* backgroundTask.pipe(Effect.forkScoped({ startImmediately: true }))
yield* Effect.log("Task forked in scope")
yield* Effect.sleep("1 second")
// Fiber will be interrupted when scope closes
return "scope completed"
})
)
forkScoped(import EffectEffect.const repeat: {
<O extends Repeat.Options<A>, A>(options: O): <
E,
R
>(
self: Effect<A, E, R>
) => Repeat.Return<R, E, A, O>
<Output, Input, Error, Env>(
schedule: Schedule<
Output,
NoInfer<Input>,
Error,
Env
>
): <E, R>(
self: Effect<Input, E, R>
) => Effect<Output, E | Error, R | Env>
<Output, Input, Error, Env>(
builder: (
$: <O, E, R>(
_: Schedule<O, NoInfer<Input>, E, R>
) => Schedule<O, Input, E, R>
) => Schedule<
Output,
NoInfer<Input>,
Error,
Env
>
): <E, R>(
self: Effect<Input, E, R>
) => Effect<Output, E | Error, R | Env>
<A, E, R, O extends Repeat.Options<A>>(
self: Effect<A, E, R>,
options: O
): Repeat.Return<R, E, A, O>
<Input, E, R, Output, Error, Env>(
self: Effect<Input, E, R>,
schedule: Schedule<
Output,
NoInfer<Input>,
Error,
Env
>
): Effect<Output, E | Error, R | Env>
<Input, E, R, Output, Error, Env>(
self: Effect<Input, E, R>,
builder: (
$: <O, E, R>(
_: Schedule<O, NoInfer<Input>, E, R>
) => Schedule<O, Input, E, R>
) => Schedule<
Output,
NoInfer<Input>,
Error,
Env
>
): Effect<Output, E | Error, R | Env>
}
repeat(const refresh: <A, E>(
self: Resource<A, E>
) => Effect.Effect<void, E>
Re-runs this resource's acquisition effect and updates the current value.
When to use
Use to force an existing Resource to reacquire its value at a
caller-controlled point.
Details
When acquisition succeeds, refreshing replaces the value stored in the
resource's scoped reference and releases resources associated with the
previous value.
Gotchas
If acquisition fails, the returned effect fails and the previously stored
result is left as what get reads.
refresh(self: Resource<A, E>(parameter) self: {
scopedRef: ScopedRef.ScopedRef<Exit.Exit<A, E>>;
acquire: Effect.Effect<A, E>;
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; <…;
}
self), policy: Schedule.Schedule<Out, unknown, E2, R2>(parameter) policy: {
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; <…;
}
policy))
)