<A, E, R>(effect: Effect.Effect<A, E, R>): (
self: TxReentrantLock
) => Effect.Effect<A, E, R>
<A, E, R>(
self: TxReentrantLock,
effect: Effect.Effect<A, E, R>
): Effect.Effect<A, E, R>Runs the provided effect while holding a write lock. The lock is automatically released after the effect completes, fails, or is interrupted.
Example (Running an effect with a write lock)
import { Effect, TxReentrantLock } from "effect"
const program = Effect.gen(function*() {
const lock = yield* TxReentrantLock.make()
const result = yield* TxReentrantLock.withWriteLock(
lock,
Effect.succeed("wrote data")
)
console.log(result) // "wrote data"
})export const const withWriteLock: {
<A, E, R>(effect: Effect.Effect<A, E, R>): (
self: TxReentrantLock
) => Effect.Effect<A, E, R>
<A, E, R>(
self: TxReentrantLock,
effect: Effect.Effect<A, E, R>
): Effect.Effect<A, E, R>
}
Runs the provided effect while holding a write lock. The lock is automatically
released after the effect completes, fails, or is interrupted.
Example (Running an effect with a write lock)
import { Effect, TxReentrantLock } from "effect"
const program = Effect.gen(function*() {
const lock = yield* TxReentrantLock.make()
const result = yield* TxReentrantLock.withWriteLock(
lock,
Effect.succeed("wrote data")
)
console.log(result) // "wrote data"
})
withWriteLock: {
<function (type parameter) A in <A, E, R>(effect: Effect.Effect<A, E, R>): (self: TxReentrantLock) => Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): (self: TxReentrantLock) => Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): (self: TxReentrantLock) => 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.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>(effect: Effect.Effect<A, E, R>): (self: TxReentrantLock) => Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): (self: TxReentrantLock) => Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): (self: TxReentrantLock) => Effect.Effect<A, E, R>R>): (self: TxReentrantLock(parameter) self: {
stateRef: TxRef.TxRef<LockState>;
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: TxReentrantLock) => 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>(effect: Effect.Effect<A, E, R>): (self: TxReentrantLock) => Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): (self: TxReentrantLock) => Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): (self: TxReentrantLock) => Effect.Effect<A, E, R>R>
<function (type parameter) A in <A, E, R>(self: TxReentrantLock, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(self: TxReentrantLock, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: TxReentrantLock, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>R>(self: TxReentrantLock(parameter) self: {
stateRef: TxRef.TxRef<LockState>;
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: TxReentrantLock, 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.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>(self: TxReentrantLock, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(self: TxReentrantLock, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: TxReentrantLock, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>R>): 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>(self: TxReentrantLock, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(self: TxReentrantLock, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: TxReentrantLock, 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 effect: anyeffect] = args: any[]args
return (self: TxReentrantLock(parameter) self: {
stateRef: TxRef.TxRef<LockState>;
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: TxReentrantLock) =>
import EffectEffect.const acquireUseRelease: <
Resource,
E,
R,
A,
E2,
R2,
E3,
R3
>(
acquire: Effect<Resource, E, R>,
use: (a: Resource) => Effect<A, E2, R2>,
release: (
a: Resource,
exit: Exit.Exit<A, E2>
) => Effect<void, E3, R3>
) => Effect<A, E | E2 | E3, R | R2 | R3>
Runs resource acquisition, usage, and release as one bracketed effect.
When to use
Use to bracket acquire, use, and release logic in one effect.
Details
acquireUseRelease does the following:
- Ensures that the
Effect value that acquires the resource will not be
interrupted. Note that acquisition may still fail due to internal
reasons (such as an uncaught exception).
- Ensures that the
release Effect value will not be interrupted,
and will be executed as long as the acquisition Effect value
successfully acquires the resource.
During the time period between the acquisition and release of the resource,
the use Effect value will be executed.
If the release Effect value fails, then the entire Effect value will
fail, even if the use Effect value succeeds. If this fail-fast behavior
is not desired, errors produced by the release Effect value can be caught
and ignored.
Example (Acquiring resources with cleanup)
import { Console, Effect, Exit } from "effect"
interface Database {
readonly connection: string
readonly query: (sql: string) => Effect.Effect<string>
}
const program = Effect.acquireUseRelease(
// Acquire - connect to database
Effect.gen(function*() {
yield* Console.log("Connecting to database...")
return {
connection: "db://localhost:5432",
query: (sql: string) => Effect.succeed(`Result for: ${sql}`)
}
}),
// Use - perform database operations
(db) =>
Effect.gen(function*() {
yield* Console.log(`Connected to ${db.connection}`)
const result = yield* db.query("SELECT * FROM users")
yield* Console.log(`Query result: ${result}`)
return result
}),
// Release - close database connection
(db, exit) =>
Effect.gen(function*() {
if (Exit.isSuccess(exit)) {
yield* Console.log(`Closing connection to ${db.connection} (success)`)
} else {
yield* Console.log(`Closing connection to ${db.connection} (failure)`)
}
})
)
Effect.runPromise(program)
// Output:
// Connecting to database...
// Connected to db://localhost:5432
// Query result: Result for: SELECT * FROM users
// Closing connection to db://localhost:5432 (success)
acquireUseRelease(
const acquireWrite: (
self: TxReentrantLock
) => Effect.Effect<number>
Acquires the write lock for the current fiber.
When to use
Use to enter an exclusive section manually when withWriteLock is not the
right shape.
Details
Blocks if any other fiber holds a read or write lock. If the current fiber
already holds the write lock, the count is incremented. If the current fiber
holds a read lock, the write lock is granted as an upgrade.
Returns the current number of write locks held by this fiber.
Example (Acquiring a write lock)
import { Effect, TxReentrantLock } from "effect"
const program = Effect.gen(function*() {
const lock = yield* TxReentrantLock.make()
const count = yield* TxReentrantLock.acquireWrite(lock)
console.log(count) // 1
yield* TxReentrantLock.releaseWrite(lock)
})
acquireWrite(self: TxReentrantLock(parameter) self: {
stateRef: TxRef.TxRef<LockState>;
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),
() => const effect: anyeffect,
() => const releaseWrite: (
self: TxReentrantLock
) => Effect.Effect<number>
Releases one write lock held by the current fiber.
When to use
Use to leave a manually acquired write lock.
Details
Returns the remaining number of write locks held by this fiber.
Example (Releasing a write lock)
import { Effect, TxReentrantLock } from "effect"
const program = Effect.gen(function*() {
const lock = yield* TxReentrantLock.make()
yield* TxReentrantLock.acquireWrite(lock)
const remaining = yield* TxReentrantLock.releaseWrite(lock)
console.log(remaining) // 0
})
releaseWrite(self: TxReentrantLock(parameter) self: {
stateRef: TxRef.TxRef<LockState>;
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)
)
}
const [const self: anyself, const effect: anyeffect] = args: any[]args
return import EffectEffect.const acquireUseRelease: <
Resource,
E,
R,
A,
E2,
R2,
E3,
R3
>(
acquire: Effect<Resource, E, R>,
use: (a: Resource) => Effect<A, E2, R2>,
release: (
a: Resource,
exit: Exit.Exit<A, E2>
) => Effect<void, E3, R3>
) => Effect<A, E | E2 | E3, R | R2 | R3>
Runs resource acquisition, usage, and release as one bracketed effect.
When to use
Use to bracket acquire, use, and release logic in one effect.
Details
acquireUseRelease does the following:
- Ensures that the
Effect value that acquires the resource will not be
interrupted. Note that acquisition may still fail due to internal
reasons (such as an uncaught exception).
- Ensures that the
release Effect value will not be interrupted,
and will be executed as long as the acquisition Effect value
successfully acquires the resource.
During the time period between the acquisition and release of the resource,
the use Effect value will be executed.
If the release Effect value fails, then the entire Effect value will
fail, even if the use Effect value succeeds. If this fail-fast behavior
is not desired, errors produced by the release Effect value can be caught
and ignored.
Example (Acquiring resources with cleanup)
import { Console, Effect, Exit } from "effect"
interface Database {
readonly connection: string
readonly query: (sql: string) => Effect.Effect<string>
}
const program = Effect.acquireUseRelease(
// Acquire - connect to database
Effect.gen(function*() {
yield* Console.log("Connecting to database...")
return {
connection: "db://localhost:5432",
query: (sql: string) => Effect.succeed(`Result for: ${sql}`)
}
}),
// Use - perform database operations
(db) =>
Effect.gen(function*() {
yield* Console.log(`Connected to ${db.connection}`)
const result = yield* db.query("SELECT * FROM users")
yield* Console.log(`Query result: ${result}`)
return result
}),
// Release - close database connection
(db, exit) =>
Effect.gen(function*() {
if (Exit.isSuccess(exit)) {
yield* Console.log(`Closing connection to ${db.connection} (success)`)
} else {
yield* Console.log(`Closing connection to ${db.connection} (failure)`)
}
})
)
Effect.runPromise(program)
// Output:
// Connecting to database...
// Connected to db://localhost:5432
// Query result: Result for: SELECT * FROM users
// Closing connection to db://localhost:5432 (success)
acquireUseRelease(
const acquireWrite: (
self: TxReentrantLock
) => Effect.Effect<number>
Acquires the write lock for the current fiber.
When to use
Use to enter an exclusive section manually when withWriteLock is not the
right shape.
Details
Blocks if any other fiber holds a read or write lock. If the current fiber
already holds the write lock, the count is incremented. If the current fiber
holds a read lock, the write lock is granted as an upgrade.
Returns the current number of write locks held by this fiber.
Example (Acquiring a write lock)
import { Effect, TxReentrantLock } from "effect"
const program = Effect.gen(function*() {
const lock = yield* TxReentrantLock.make()
const count = yield* TxReentrantLock.acquireWrite(lock)
console.log(count) // 1
yield* TxReentrantLock.releaseWrite(lock)
})
acquireWrite(const self: anyself),
() => const effect: anyeffect,
() => const releaseWrite: (
self: TxReentrantLock
) => Effect.Effect<number>
Releases one write lock held by the current fiber.
When to use
Use to leave a manually acquired write lock.
Details
Returns the remaining number of write locks held by this fiber.
Example (Releasing a write lock)
import { Effect, TxReentrantLock } from "effect"
const program = Effect.gen(function*() {
const lock = yield* TxReentrantLock.make()
yield* TxReentrantLock.acquireWrite(lock)
const remaining = yield* TxReentrantLock.releaseWrite(lock)
console.log(remaining) // 0
})
releaseWrite(const self: anyself)
)
}) as any