(self: TxSemaphore): <A, E, R>(
effect: Effect.Effect<A, E, R>
) => Effect.Effect<A, E, R>
<A, E, R>(
self: TxSemaphore,
effect: Effect.Effect<A, E, R>
): Effect.Effect<A, E, R>Executes an effect with a single permit from the semaphore. The permit is automatically acquired before execution and released afterwards, even if the effect fails or is interrupted.
When to use
Use to run an effect while automatically acquiring and releasing one transactional permit.
Details
The permit acquisition and release operations use atomic semantics to ensure proper resource management with Effect's scoped operations.
Example (Running an effect with a permit)
import { Console, Effect, TxSemaphore } from "effect"
const program = Effect.gen(function*() {
const semaphore = yield* TxSemaphore.make(2)
// Execute database operation with automatic permit management
const result = yield* TxSemaphore.withPermit(
semaphore,
Effect.gen(function*() {
yield* Console.log("Permit acquired, accessing database...")
yield* Effect.sleep("100 millis") // Simulate database work
yield* Console.log("Database operation complete")
return "query result"
})
)
yield* Console.log(`Result: ${result}`)
// Permit is automatically released here
})export const const withPermit: {
(self: TxSemaphore): <A, E, R>(
effect: Effect.Effect<A, E, R>
) => Effect.Effect<A, E, R>
<A, E, R>(
self: TxSemaphore,
effect: Effect.Effect<A, E, R>
): Effect.Effect<A, E, R>
}
Executes an effect with a single permit from the semaphore. The permit is
automatically acquired before execution and released afterwards, even if the
effect fails or is interrupted.
When to use
Use to run an effect while automatically acquiring and releasing one
transactional permit.
Details
The permit acquisition and release operations use atomic semantics to ensure
proper resource management with Effect's scoped operations.
Example (Running an effect with a permit)
import { Console, Effect, TxSemaphore } from "effect"
const program = Effect.gen(function*() {
const semaphore = yield* TxSemaphore.make(2)
// Execute database operation with automatic permit management
const result = yield* TxSemaphore.withPermit(
semaphore,
Effect.gen(function*() {
yield* Console.log("Permit acquired, accessing database...")
yield* Effect.sleep("100 millis") // Simulate database work
yield* Console.log("Database operation complete")
return "query result"
})
)
yield* Console.log(`Result: ${result}`)
// Permit is automatically released here
})
withPermit: {
(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): <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, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(self: TxSemaphore, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: TxSemaphore, 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, 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, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(self: TxSemaphore, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: TxSemaphore, 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, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(self: TxSemaphore, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: TxSemaphore, 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 === 1) {
const [const self: anyself] = 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 acquire: (
self: TxSemaphore
) => Effect.Effect<void>
Acquires a single permit from the semaphore. If no permits are available,
the effect will block until one becomes available.
When to use
Use to manually acquire one permit transactionally, waiting until one is
available.
Example (Acquiring a permit)
import { Console, Effect, TxSemaphore } from "effect"
const program = Effect.gen(function*() {
const semaphore = yield* TxSemaphore.make(2)
yield* Console.log("Acquiring first permit...")
yield* TxSemaphore.acquire(semaphore)
yield* Console.log("First permit acquired")
yield* Console.log("Acquiring second permit...")
yield* TxSemaphore.acquire(semaphore)
yield* Console.log("Second permit acquired")
const available = yield* TxSemaphore.available(semaphore)
yield* Console.log(`Available permits: ${available}`) // 0
})
acquire(const self: anyself),
() => 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 release: (
self: TxSemaphore
) => Effect.Effect<void>
Releases one permit back to the semaphore, making it available for
acquisition.
When to use
Use to manually return one permit after a transactional acquire.
Details
If the semaphore is already at capacity, this operation leaves the permit
count unchanged.
Example (Releasing a permit)
import { Console, Effect, TxSemaphore } from "effect"
const program = Effect.gen(function*() {
const semaphore = yield* TxSemaphore.make(2)
// Acquire a permit
yield* TxSemaphore.acquire(semaphore)
let available = yield* TxSemaphore.available(semaphore)
yield* Console.log(`After acquire: ${available}`) // 1
// Release the permit
yield* TxSemaphore.release(semaphore)
available = yield* TxSemaphore.available(semaphore)
yield* Console.log(`After release: ${available}`) // 2
})
release(const self: anyself)
)
}
const [const self: anyself, const effect: anyeffect] = args: any[]args
return import EffectEffect.acquireUseRelease(
const acquire: (
self: TxSemaphore
) => Effect.Effect<void>
Acquires a single permit from the semaphore. If no permits are available,
the effect will block until one becomes available.
When to use
Use to manually acquire one permit transactionally, waiting until one is
available.
Example (Acquiring a permit)
import { Console, Effect, TxSemaphore } from "effect"
const program = Effect.gen(function*() {
const semaphore = yield* TxSemaphore.make(2)
yield* Console.log("Acquiring first permit...")
yield* TxSemaphore.acquire(semaphore)
yield* Console.log("First permit acquired")
yield* Console.log("Acquiring second permit...")
yield* TxSemaphore.acquire(semaphore)
yield* Console.log("Second permit acquired")
const available = yield* TxSemaphore.available(semaphore)
yield* Console.log(`Available permits: ${available}`) // 0
})
acquire(const self: anyself),
() => const effect: anyeffect,
() => const release: (
self: TxSemaphore
) => Effect.Effect<void>
Releases one permit back to the semaphore, making it available for
acquisition.
When to use
Use to manually return one permit after a transactional acquire.
Details
If the semaphore is already at capacity, this operation leaves the permit
count unchanged.
Example (Releasing a permit)
import { Console, Effect, TxSemaphore } from "effect"
const program = Effect.gen(function*() {
const semaphore = yield* TxSemaphore.make(2)
// Acquire a permit
yield* TxSemaphore.acquire(semaphore)
let available = yield* TxSemaphore.available(semaphore)
yield* Console.log(`After acquire: ${available}`) // 1
// Release the permit
yield* TxSemaphore.release(semaphore)
available = yield* TxSemaphore.available(semaphore)
yield* Console.log(`After release: ${available}`) // 2
})
release(const self: anyself)
)
}) as any