(self: TxSemaphore, n: number): Effect.Effect<boolean>Tries to acquire the specified number of permits from the semaphore without
blocking, returning true if successful or false if not enough permits are
available.
When to use
Use to attempt a multi-permit acquisition without retrying when not enough permits are available.
Example (Trying to acquire multiple permits)
import { Console, Effect, TxSemaphore } from "effect"
const program = Effect.gen(function*() {
const semaphore = yield* TxSemaphore.make(3)
// Try to acquire 2 permits (should succeed)
const first = yield* TxSemaphore.tryAcquireN(semaphore, 2)
yield* Console.log(`First try (2 permits): ${first}`) // true
// Try to acquire 2 more permits (should fail, only 1 left)
const second = yield* TxSemaphore.tryAcquireN(semaphore, 2)
yield* Console.log(`Second try (2 permits): ${second}`) // false
})export const const tryAcquireN: (
self: TxSemaphore,
n: number
) => Effect.Effect<boolean>
Tries to acquire the specified number of permits from the semaphore without
blocking, returning true if successful or false if not enough permits are
available.
When to use
Use to attempt a multi-permit acquisition without retrying when not enough
permits are available.
Example (Trying to acquire multiple permits)
import { Console, Effect, TxSemaphore } from "effect"
const program = Effect.gen(function*() {
const semaphore = yield* TxSemaphore.make(3)
// Try to acquire 2 permits (should succeed)
const first = yield* TxSemaphore.tryAcquireN(semaphore, 2)
yield* Console.log(`First try (2 permits): ${first}`) // true
// Try to acquire 2 more permits (should fail, only 1 left)
const second = yield* TxSemaphore.tryAcquireN(semaphore, 2)
yield* Console.log(`Second try (2 permits): ${second}`) // false
})
tryAcquireN = (self: TxSemaphore(parameter) self: {
permitsRef: TxRef.TxRef<number>;
capacity: number;
toString: () => string;
toJSON: () => unknown;
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: TxSemaphore, n: numbern: number): 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<boolean> => {
if (n: numbern <= 0) {
return import EffectEffect.const die: (
defect: unknown
) => Effect<never>
Creates an effect that terminates a fiber with a specified error.
When to use
Use when you need an Effect to report an unrecoverable defect instead of a
typed error.
Details
The die function is used to signal a defect, which represents a critical
and unexpected error in the code. When invoked, it produces an effect that
does not handle the error and instead terminates the fiber.
The error channel of the resulting effect is of type never, indicating that
it cannot recover from this failure.
Example (Failing on division by zero)
import { Effect } from "effect"
const divide = (a: number, b: number) =>
b === 0
? Effect.die(new Error("Cannot divide by zero"))
: Effect.succeed(a / b)
// ┌─── Effect<number, never, never>
// ▼
const program = divide(1, 0)
Effect.runPromise(program).catch(console.error)
// Output:
// (FiberFailure) Error: Cannot divide by zero
// ...stack trace...
die(new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error("Number of permits must be positive"))
}
return import TxRefTxRef.const modify: {
<A, R>(
f: (
current: NoInfer<A>
) => [returnValue: R, newValue: A]
): (self: TxRef<A>) => Effect.Effect<R>
<A, R>(
self: TxRef<A>,
f: (
current: A
) => [returnValue: R, newValue: A]
): Effect.Effect<R>
}
modify(self: TxSemaphore(parameter) self: {
permitsRef: TxRef.TxRef<number>;
capacity: number;
toString: () => string;
toJSON: () => unknown;
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.TxSemaphore.permitsRef: TxRef.TxRef<number>(property) TxSemaphore.permitsRef: {
version: number;
pending: Map<unknown, () => void>;
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; <…;
}
permitsRef, (permits: numberpermits: number) => {
if (permits: numberpermits >= n: numbern) {
return [true, permits: numberpermits - n: numbern]
}
return [false, permits: numberpermits]
})
}