(self: TxSemaphore, n: number): <A, E, R>(
effect: Effect.Effect<A, E, R>
) => Effect.Effect<A, E, R>
<A, E, R>(
self: TxSemaphore,
n: number,
effect: Effect.Effect<A, E, R>
): Effect.Effect<A, E, R>Runs an effect while holding the specified number of permits from the semaphore.
When to use
Use to run an effect while automatically acquiring and releasing multiple transactional permits.
Details
The permits are acquired before the effect starts and released after it completes, fails, or is interrupted.
Gotchas
Passing a non-positive n dies with a defect. Passing a value greater than
the semaphore capacity can wait forever.
Example (Running an effect with multiple permits)
import { Console, Effect, TxSemaphore } from "effect"
const program = Effect.gen(function*() {
const semaphore = yield* TxSemaphore.make(5)
// Execute batch operation with 3 permits
const results = yield* TxSemaphore.withPermits(
semaphore,
3,
Effect.gen(function*() {
yield* Console.log("3 permits acquired, processing batch...")
yield* Effect.sleep("200 millis") // Simulate batch processing
return ["result1", "result2", "result3"]
})
)
yield* Console.log(`Batch results: ${results.join(", ")}`)
// All 3 permits are automatically released here
})export const const withPermits: {
(self: TxSemaphore, n: number): <A, E, R>(
effect: Effect.Effect<A, E, R>
) => Effect.Effect<A, E, R>
<A, E, R>(
self: TxSemaphore,
n: number,
effect: Effect.Effect<A, E, R>
): Effect.Effect<A, E, R>
}
Runs an effect while holding the specified number of permits from the
semaphore.
When to use
Use to run an effect while automatically acquiring and releasing multiple
transactional permits.
Details
The permits are acquired before the effect starts and released after it
completes, fails, or is interrupted.
Gotchas
Passing a non-positive n dies with a defect. Passing a value greater than
the semaphore capacity can wait forever.
Example (Running an effect with multiple permits)
import { Console, Effect, TxSemaphore } from "effect"
const program = Effect.gen(function*() {
const semaphore = yield* TxSemaphore.make(5)
// Execute batch operation with 3 permits
const results = yield* TxSemaphore.withPermits(
semaphore,
3,
Effect.gen(function*() {
yield* Console.log("3 permits acquired, processing batch...")
yield* Effect.sleep("200 millis") // Simulate batch processing
return ["result1", "result2", "result3"]
})
)
yield* Console.log(`Batch results: ${results.join(", ")}`)
// All 3 permits are automatically released here
})
withPermits: {
(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): <function (type parameter) A in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>R>(effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect: import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>R>) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>R>
<function (type parameter) A in <A, E, R>(self: TxSemaphore, n: number, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(self: TxSemaphore, n: number, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: TxSemaphore, n: number, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>R>(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, effect: Effect.Effect<A, E, R>(parameter) effect: {
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;
}
effect: import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <A, E, R>(self: TxSemaphore, n: number, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(self: TxSemaphore, n: number, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: TxSemaphore, n: number, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>R>): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <A, E, R>(self: TxSemaphore, n: number, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(self: TxSemaphore, n: number, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: TxSemaphore, n: number, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>R>
} = ((...args: any[]args: interface Array<T>Array<any>) => {
if (args: any[]args.Array<any>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length === 2) {
const [const self: anyself, const n: anyn] = args: any[]args
return (effect: Effect.Effect<any, any, any>(parameter) effect: {
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;
}
effect: import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<any, any, any>) =>
import EffectEffect.acquireUseRelease(
const acquireN: (
self: TxSemaphore,
n: number
) => Effect.Effect<void>
Acquires the specified number of permits from the semaphore.
When to use
Use to manually acquire multiple permits transactionally, waiting until all
requested permits are available.
Details
If fewer than n permits are available, the transaction retries until enough
permits are released.
Gotchas
Passing a non-positive n dies with a defect. Passing a value greater than
the semaphore capacity can wait forever because the capacity is fixed.
Example (Acquiring multiple permits)
import { Console, Effect, TxSemaphore } from "effect"
const program = Effect.gen(function*() {
const semaphore = yield* TxSemaphore.make(5)
yield* Console.log("Acquiring 3 permits...")
yield* TxSemaphore.acquireN(semaphore, 3)
yield* Console.log("3 permits acquired")
const available = yield* TxSemaphore.available(semaphore)
yield* Console.log(`Available permits: ${available}`) // 2
})
acquireN(const self: anyself, const n: anyn),
() => effect: Effect.Effect<any, any, any>(parameter) effect: {
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;
}
effect,
() => const releaseN: (
self: TxSemaphore,
n: number
) => Effect.Effect<void>
Releases the specified number of permits back to the semaphore.
When to use
Use to manually return multiple permits after a transactional acquire.
Details
The available permit count is capped at the semaphore capacity.
Gotchas
Passing a non-positive n dies with a defect.
Example (Releasing multiple permits)
import { Console, Effect, TxSemaphore } from "effect"
const program = Effect.gen(function*() {
const semaphore = yield* TxSemaphore.make(5)
// Acquire 3 permits
yield* TxSemaphore.acquireN(semaphore, 3)
let available = yield* TxSemaphore.available(semaphore)
yield* Console.log(`After acquire: ${available}`) // 2
// Release 2 permits
yield* TxSemaphore.releaseN(semaphore, 2)
available = yield* TxSemaphore.available(semaphore)
yield* Console.log(`After release: ${available}`) // 4
})
releaseN(const self: anyself, const n: anyn)
)
}
const [const self: anyself, const n: anyn, const effect: anyeffect] = args: any[]args
return import EffectEffect.acquireUseRelease(
const acquireN: (
self: TxSemaphore,
n: number
) => Effect.Effect<void>
Acquires the specified number of permits from the semaphore.
When to use
Use to manually acquire multiple permits transactionally, waiting until all
requested permits are available.
Details
If fewer than n permits are available, the transaction retries until enough
permits are released.
Gotchas
Passing a non-positive n dies with a defect. Passing a value greater than
the semaphore capacity can wait forever because the capacity is fixed.
Example (Acquiring multiple permits)
import { Console, Effect, TxSemaphore } from "effect"
const program = Effect.gen(function*() {
const semaphore = yield* TxSemaphore.make(5)
yield* Console.log("Acquiring 3 permits...")
yield* TxSemaphore.acquireN(semaphore, 3)
yield* Console.log("3 permits acquired")
const available = yield* TxSemaphore.available(semaphore)
yield* Console.log(`Available permits: ${available}`) // 2
})
acquireN(const self: anyself, const n: anyn),
() => const effect: anyeffect,
() => const releaseN: (
self: TxSemaphore,
n: number
) => Effect.Effect<void>
Releases the specified number of permits back to the semaphore.
When to use
Use to manually return multiple permits after a transactional acquire.
Details
The available permit count is capped at the semaphore capacity.
Gotchas
Passing a non-positive n dies with a defect.
Example (Releasing multiple permits)
import { Console, Effect, TxSemaphore } from "effect"
const program = Effect.gen(function*() {
const semaphore = yield* TxSemaphore.make(5)
// Acquire 3 permits
yield* TxSemaphore.acquireN(semaphore, 3)
let available = yield* TxSemaphore.available(semaphore)
yield* Console.log(`After acquire: ${available}`) // 2
// Release 2 permits
yield* TxSemaphore.releaseN(semaphore, 2)
available = yield* TxSemaphore.available(semaphore)
yield* Console.log(`After release: ${available}`) // 4
})
releaseN(const self: anyself, const n: anyn)
)
}) as any