<A>(item: A): <E>(
self: Pool<A, E>
) => Effect.Effect<void, never, Scope.Scope>
<A, E>(self: Pool<A, E>, item: A): Effect.Effect<void, never, Scope.Scope>Invalidates the specified item so the pool can remove it and reallocate the item, lazily if needed.
When to use
Use to prevent a pooled item from being reused after it becomes unsuitable, such as a stale connection or a resource that failed a health check.
Gotchas
The item is matched with strict equality. Passing an equivalent but different object instance does nothing.
export const const invalidate: {
<A>(item: A): <E>(
self: Pool<A, E>
) => Effect.Effect<void, never, Scope.Scope>
<A, E>(
self: Pool<A, E>,
item: A
): Effect.Effect<void, never, Scope.Scope>
}
Invalidates the specified item so the pool can remove it and reallocate the
item, lazily if needed.
When to use
Use to prevent a pooled item from being reused after it becomes unsuitable,
such as a stale connection or a resource that failed a health check.
Gotchas
The item is matched with strict equality. Passing an equivalent but different
object instance does nothing.
invalidate: {
<function (type parameter) A in <A>(item: A): <E>(self: Pool<A, E>) => Effect.Effect<void, never, Scope.Scope>A>(item: Aitem: function (type parameter) A in <A>(item: A): <E>(self: Pool<A, E>) => Effect.Effect<void, never, Scope.Scope>A): <function (type parameter) E in <E>(self: Pool<A, E>): Effect.Effect<void, never, Scope.Scope>E>(self: Pool<A, E>(parameter) self: {
config: Config<A, E>;
state: State<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: interface Pool<in out A, in out E = never>A Pool<A, E> is a pool of items of type A, each of which may be
associated with the acquisition and release of resources. An attempt to get
an item A from a pool may fail with an error of type E.
When to use
Use when you need to share a bounded set of scoped resources across fibers
while the pool manages acquisition, reuse, and release.
Pool<function (type parameter) A in <A>(item: A): <E>(self: Pool<A, E>) => Effect.Effect<void, never, Scope.Scope>A, function (type parameter) E in <E>(self: Pool<A, E>): Effect.Effect<void, never, Scope.Scope>E>) => 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<void, never, import ScopeScope.Scope>
<function (type parameter) A in <A, E>(self: Pool<A, E>, item: A): Effect.Effect<void, never, Scope.Scope>A, function (type parameter) E in <A, E>(self: Pool<A, E>, item: A): Effect.Effect<void, never, Scope.Scope>E>(self: Pool<A, E>(parameter) self: {
config: Config<A, E>;
state: State<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: interface Pool<in out A, in out E = never>A Pool<A, E> is a pool of items of type A, each of which may be
associated with the acquisition and release of resources. An attempt to get
an item A from a pool may fail with an error of type E.
When to use
Use when you need to share a bounded set of scoped resources across fibers
while the pool manages acquisition, reuse, and release.
Pool<function (type parameter) A in <A, E>(self: Pool<A, E>, item: A): Effect.Effect<void, never, Scope.Scope>A, function (type parameter) E in <A, E>(self: Pool<A, E>, item: A): Effect.Effect<void, never, Scope.Scope>E>, item: Aitem: function (type parameter) A in <A, E>(self: Pool<A, E>, item: A): Effect.Effect<void, never, Scope.Scope>A): 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<void, never, import ScopeScope.Scope>
} = dual<(...args: Array<any>) => any, <A, E>(self: Pool<A, E>, item: A) => Effect.Effect<void, never, Scope.Scope>>(arity: 2, body: <A, E>(self: Pool<A, E>, item: A) => Effect.Effect<void, never, Scope.Scope>): ((...args: Array<any>) => any) & (<A, E>(self: Pool<A, E>, item: A) => Effect.Effect<void, never, Scope.Scope>) (+1 overload)Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual(2, <function (type parameter) A in <A, E>(self: Pool<A, E>, item: A): Effect.Effect<void, never, Scope.Scope>A, function (type parameter) E in <A, E>(self: Pool<A, E>, item: A): Effect.Effect<void, never, Scope.Scope>E>(self: Pool<A, E>(parameter) self: {
config: Config<A, E>;
state: State<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: interface Pool<in out A, in out E = never>A Pool<A, E> is a pool of items of type A, each of which may be
associated with the acquisition and release of resources. An attempt to get
an item A from a pool may fail with an error of type E.
When to use
Use when you need to share a bounded set of scoped resources across fibers
while the pool manages acquisition, reuse, and release.
Pool<function (type parameter) A in <A, E>(self: Pool<A, E>, item: A): Effect.Effect<void, never, Scope.Scope>A, function (type parameter) E in <A, E>(self: Pool<A, E>, item: A): Effect.Effect<void, never, Scope.Scope>E>, item: Aitem: function (type parameter) A in <A, E>(self: Pool<A, E>, item: A): Effect.Effect<void, never, Scope.Scope>A): 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<void, never, import ScopeScope.Scope> =>
import EffectEffect.const suspend: <A, E, R>(
effect: LazyArg<Effect<A, E, R>>
) => Effect<A, E, R>
Creates an Effect lazily, delaying construction until it is needed.
When to use
Use when you need to defer the evaluation of an effect until it is required.
Details
suspend takes a thunk that represents an effect and delays creating it
until the suspended effect is evaluated. This is useful for optimizing
expensive computations, managing circular dependencies such as recursive
functions, and helping TypeScript unify return types when branches construct
different effects. Any side effects or scoped captures inside the thunk are
re-executed on each invocation.
Example (Lazily evaluating side effects)
import { Effect } from "effect"
let i = 0
const bad = Effect.succeed(i++)
const good = Effect.suspend(() => Effect.succeed(i++))
console.log(Effect.runSync(bad)) // Output: 0
console.log(Effect.runSync(bad)) // Output: 0
console.log(Effect.runSync(good)) // Output: 1
console.log(Effect.runSync(good)) // Output: 2
Example (Suspending recursive Fibonacci evaluation)
import { Effect } from "effect"
const blowsUp = (n: number): Effect.Effect<number> =>
n < 2
? Effect.succeed(1)
: Effect.zipWith(blowsUp(n - 1), blowsUp(n - 2), (a, b) => a + b)
// console.log(Effect.runSync(blowsUp(32)))
// crash: JavaScript heap out of memory
const allGood = (n: number): Effect.Effect<number> =>
n < 2
? Effect.succeed(1)
: Effect.zipWith(
Effect.suspend(() => allGood(n - 1)),
Effect.suspend(() => allGood(n - 2)),
(a, b) => a + b
)
console.log(Effect.runSync(allGood(32)))
// Output: 3524578
Example (Helping TypeScript infer recursive effect types)
import { Effect } from "effect"
// Without suspend, TypeScript may struggle with type inference.
// Inferred type:
// (a: number, b: number) =>
// Effect<never, Error, never> | Effect<number, never, never>
const withoutSuspend = (a: number, b: number) =>
b === 0
? Effect.fail(new Error("Cannot divide by zero"))
: Effect.succeed(a / b)
// Using suspend to unify return types.
// Inferred type:
// (a: number, b: number) => Effect<number, Error, never>
const withSuspend = (a: number, b: number) =>
Effect.suspend(() =>
b === 0
? Effect.fail(new Error("Cannot divide by zero"))
: Effect.succeed(a / b)
)
suspend(() => {
if (self: Pool<A, E>(parameter) self: {
config: Config<A, E>;
state: State<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.Pool<A, E>.state: State<A, E>(property) Pool<A, E>.state: {
scope: Scope.Scope;
isShuttingDown: boolean;
semaphore: Semaphore.Semaphore;
resizeSemaphore: Semaphore.Semaphore;
items: Set<PoolItem<A, E>>;
available: Set<PoolItem<A, E>>;
availableLatch: Latch.Latch;
invalidated: Set<PoolItem<A, E>>;
waiters: number;
}
state.State<A, E>.isShuttingDown: booleanisShuttingDown) return import EffectEffect.const void: Effect.Effect<void, never, never>(alias) const void: {
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;
}
Returns an effect that succeeds with void.
void
for (const const poolItem: PoolItem<A, E>const poolItem: {
exit: Exit.Exit<A, E>;
finalizer: Effect.Effect<void>;
refCount: number;
disableReclaim: boolean;
}
poolItem of self: Pool<A, E>(parameter) self: {
config: Config<A, E>;
state: State<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.Pool<A, E>.state: State<A, E>(property) Pool<A, E>.state: {
scope: Scope.Scope;
isShuttingDown: boolean;
semaphore: Semaphore.Semaphore;
resizeSemaphore: Semaphore.Semaphore;
items: Set<PoolItem<A, E>>;
available: Set<PoolItem<A, E>>;
availableLatch: Latch.Latch;
invalidated: Set<PoolItem<A, E>>;
waiters: number;
}
state.State<A, E>.items: Set<PoolItem<A, E>>items) {
if (const poolItem: PoolItem<A, E>const poolItem: {
exit: Exit.Exit<A, E>;
finalizer: Effect.Effect<void>;
refCount: number;
disableReclaim: boolean;
}
poolItem.PoolItem<A, E>.exit: Exit.Exit<A, E>exit._tag: "Success" | "Failure"_tag === "Success" && const poolItem: PoolItem<A, E>const poolItem: {
exit: Exit.Exit<A, E>;
finalizer: Effect.Effect<void>;
refCount: number;
disableReclaim: boolean;
}
poolItem.PoolItem<A, E>.exit: Exit.Exit<A, E>(property) PoolItem<A, E>.exit: {
_tag: "Success";
value: A;
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;
}
exit.Success<A, E>.value: Avalue === item: Aitem) {
const poolItem: PoolItem<A, E>const poolItem: {
exit: Exit.Exit<A, E>;
finalizer: Effect.Effect<void>;
refCount: number;
disableReclaim: boolean;
}
poolItem.PoolItem<A, E>.disableReclaim: booleandisableReclaim = true
return import EffectEffect.const uninterruptible: <A, E, R>(
self: Effect<A, E, R>
) => Effect<A, E, R>
Returns a new effect that disables interruption for the given effect.
Example (Preventing interruption)
import { Console, Effect, Fiber } from "effect"
const criticalTask = Effect.gen(function*() {
yield* Console.log("Starting critical section...")
yield* Effect.sleep("2 seconds")
yield* Console.log("Critical section completed")
})
const program = Effect.uninterruptible(criticalTask)
const fiber = Effect.runFork(program)
// Even if interrupted, the critical task will complete
Effect.runPromise(Fiber.interrupt(fiber))
uninterruptible(const invalidatePoolItem: <A, E>(
self: Pool<A, E>,
poolItem: PoolItem<A, E>
) => Effect.Effect<void>
invalidatePoolItem(self: Pool<A, E>(parameter) self: {
config: Config<A, E>;
state: State<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, const poolItem: PoolItem<A, E>const poolItem: {
exit: Exit.Exit<A, E>;
finalizer: Effect.Effect<void>;
refCount: number;
disableReclaim: boolean;
}
poolItem))
}
}
return import EffectEffect.const void: Effect.Effect<void, never, never>(alias) const void: {
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;
}
Returns an effect that succeeds with void.
void
}))