<A = never, E = never>(capacity: number): Effect.Effect<TxQueue<A, E>>Creates a new bounded TxQueue with the specified capacity.
Details
This function returns a new TxQueue reference with the specified capacity. No existing TxQueue instances are modified.
Example (Creating bounded queues)
import { Effect, TxQueue } from "effect"
const program = Effect.gen(function*() {
// Create a bounded queue (E defaults to never)
const queue = yield* TxQueue.bounded<number>(10)
// Create a bounded queue with error channel
const faultTolerantQueue = yield* TxQueue.bounded<number, string>(10)
// Offer items - will succeed until capacity is reached
yield* TxQueue.offer(queue, 1)
yield* TxQueue.offer(queue, 2)
const item = yield* TxQueue.take(queue)
console.log(item) // 1
})export const const bounded: <A = never, E = never>(
capacity: number
) => Effect.Effect<TxQueue<A, E>>
Creates a new bounded TxQueue with the specified capacity.
Details
This function returns a new TxQueue reference with the specified capacity. No existing TxQueue instances are modified.
Example (Creating bounded queues)
import { Effect, TxQueue } from "effect"
const program = Effect.gen(function*() {
// Create a bounded queue (E defaults to never)
const queue = yield* TxQueue.bounded<number>(10)
// Create a bounded queue with error channel
const faultTolerantQueue = yield* TxQueue.bounded<number, string>(10)
// Offer items - will succeed until capacity is reached
yield* TxQueue.offer(queue, 1)
yield* TxQueue.offer(queue, 2)
const item = yield* TxQueue.take(queue)
console.log(item) // 1
})
bounded = <function (type parameter) A in <A = never, E = never>(capacity: number): Effect.Effect<TxQueue<A, E>>A = never, function (type parameter) E in <A = never, E = never>(capacity: number): Effect.Effect<TxQueue<A, E>>E = never>(
capacity: numbercapacity: 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<interface TxQueue<in out A, in out E = never>Namespace containing type definitions for TxQueue variance annotations.
A TxQueue represents a transactional queue data structure that provides both
enqueue and dequeue operations with Software Transactional Memory (STM) semantics.
Example (Combining enqueue and dequeue operations)
import { Effect, TxQueue } from "effect"
const program = Effect.gen(function*() {
// Create a bounded transactional queue (E defaults to never)
const queue = yield* TxQueue.bounded<number>(10)
// Single operations - automatically transactional
const accepted = yield* TxQueue.offer(queue, 42)
const item = yield* TxQueue.take(queue) // Effect<number, never>
console.log(item) // 42
// Queue with error channel
const faultTolerantQueue = yield* TxQueue.bounded<number, string>(10)
// Operations can handle queue-level failures
yield* TxQueue.fail(faultTolerantQueue, "queue failed")
const result = yield* Effect.flip(TxQueue.take(faultTolerantQueue))
console.log(result) // "queue failed"
})
TxQueue<function (type parameter) A in <A = never, E = never>(capacity: number): Effect.Effect<TxQueue<A, E>>A, function (type parameter) E in <A = never, E = never>(capacity: number): Effect.Effect<TxQueue<A, E>>E>> =>
import EffectEffect.const gen: {
<Eff extends Effect<any, any, any>, AEff>(
f: () => Generator<Eff, AEff, never>
): Effect<
AEff,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer E, infer _R>
]
? E
: never,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer _E, infer R>
]
? R
: never
>
<Self, Eff extends Effect<any, any, any>, AEff>(
options: { readonly self: Self },
f: (this: Self) => Generator<Eff, AEff, never>
): Effect<
AEff,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer E, infer _R>
]
? E
: never,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer _E, infer R>
]
? R
: never
>
}
gen(function*() {
const const items: TxChunk.TxChunk<A>const items: {
ref: TxRef.TxRef<Chunk.Chunk<A>>;
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; <…;
}
items = yield* import TxChunkTxChunk.const empty: <
A = never
>() => Effect.Effect<TxChunk<A>>
Creates a new empty TxChunk.
Details
This function returns a new TxChunk reference that is initially empty. No existing TxChunk
instances are modified.
Example (Creating an empty TxChunk)
import { Effect, TxChunk } from "effect"
const program = Effect.gen(function*() {
// Create an empty TxChunk
const txChunk = yield* TxChunk.empty<number>()
// Check if it's empty - automatically transactional
const isEmpty = yield* TxChunk.isEmpty(txChunk)
console.log(isEmpty) // true
// Add elements - automatically transactional
yield* TxChunk.append(txChunk, 42)
const isStillEmpty = yield* TxChunk.isEmpty(txChunk)
console.log(isStillEmpty) // false
})
empty<function (type parameter) A in <A = never, E = never>(capacity: number): Effect.Effect<TxQueue<A, E>>A>()
const const stateRef: TxRef.TxRef<State<A, E>>const stateRef: {
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; <…;
}
stateRef = yield* import TxRefTxRef.const make: <State<A, E>>(initial: State<A, E>) => Effect.Effect<TxRef.TxRef<State<A, E>>, never, never>Creates a new TxRef with the specified initial value.
When to use
Use to create a TxRef inside an Effect workflow.
Example (Creating transactional references)
import { Effect, TxRef } from "effect"
const program = Effect.gen(function*() {
// Create a transactional reference with initial value
const counter = yield* TxRef.make(0)
const name = yield* TxRef.make("Alice")
// Use in transactions
yield* Effect.tx(Effect.gen(function*() {
yield* TxRef.set(counter, 42)
yield* TxRef.set(name, "Bob")
}))
console.log(yield* TxRef.get(counter)) // 42
console.log(yield* TxRef.get(name)) // "Bob"
})
make<type State<_A, E> =
| {
readonly _tag: "Open"
}
| {
readonly _tag: "Closing"
readonly cause: Cause.Cause<E>
}
| {
readonly _tag: "Done"
readonly cause: Cause.Cause<E>
}
Represents the state of a transactional queue with sophisticated lifecycle management.
Details
The queue progresses through three states:
- Open: Accepting offers and serving takes normally
- Closing: No new offers accepted, serving remaining items until empty
- Done: Terminal state with completion cause, no further operations possible
Example (Inspecting queue lifecycle states)
import type { TxQueue } from "effect"
// State progression example
declare const state: TxQueue.State<string, Error>
if (state._tag === "Open") {
console.log("Queue is accepting new items")
} else if (state._tag === "Closing") {
console.log("Queue is draining, cause:", state.cause)
} else {
console.log("Queue is done, cause:", state.cause)
}
State<function (type parameter) A in <A = never, E = never>(capacity: number): Effect.Effect<TxQueue<A, E>>A, function (type parameter) E in <A = never, E = never>(capacity: number): Effect.Effect<TxQueue<A, E>>E>>({ _tag: "Open"_tag: "Open" })
const const txQueue: anytxQueue = var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.create(o: object | null): any (+1 overload)Creates an object that has the specified prototype or that has null prototype.
create(const TxQueueProto: {
"~effect/transactions/TxQueue/Enqueue": {
_A: (_: never) => never
_E: (_: never) => never
}
"~effect/transactions/TxQueue/Dequeue": {
_A: (_: never) => never
_E: (_: never) => never
}
"~effect/transactions/TxQueue": {
_A: (_: never) => never
_E: (_: never) => never
}
[NodeInspectSymbol](
this: TxQueue<unknown, unknown>
): unknown
toString(
this: TxQueue<unknown, unknown>
): string
toJSON(this: TxQueue<unknown, unknown>): {
_id: string
strategy:
| "bounded"
| "unbounded"
| "dropping"
| "sliding"
capacity: number
}
}
TxQueueProto)
const txQueue: anytxQueue.strategy = "bounded"
const txQueue: anytxQueue.capacity = capacity: numbercapacity
const txQueue: anytxQueue.items = const items: TxChunk.TxChunk<A>const items: {
ref: TxRef.TxRef<Chunk.Chunk<A>>;
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; <…;
}
items
const txQueue: anytxQueue.stateRef = const stateRef: TxRef.TxRef<State<A, E>>const stateRef: {
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; <…;
}
stateRef
return const txQueue: anytxQueue
}).Pipeable.pipe<Effect.Effect<any, never, never>, Effect.Effect<any, never, never>>(this: Effect.Effect<any, never, never>, ab: (_: Effect.Effect<any, never, never>) => Effect.Effect<any, never, never>): Effect.Effect<any, never, never> (+21 overloads)pipe(import EffectEffect.const tx: <A, E, R>(
effect: Effect<A, E, R>
) => Effect<A, E, Exclude<R, Transaction>>
Defines a transaction boundary. Transactions are "all or nothing" with respect to changes
made to transactional values (i.e. TxRef) that occur within the transaction body.
Details
If called inside an active transaction, tx composes with the current transaction and reuses
its journal and retry state instead of creating a nested boundary.
Effect transactions are optimistic with retry. A transaction is retried when
its body explicitly calls Effect.txRetry and any accessed transactional
value changes, or when any accessed transactional value changes because a
different transaction commits before the current one.
The outermost tx call creates the transaction boundary and commits or rolls back the full
composed transaction.
Example (Running a transaction)
import { Effect, TxRef } from "effect"
const program = Effect.gen(function*() {
const ref1 = yield* TxRef.make(0)
const ref2 = yield* TxRef.make(0)
// Nested tx calls compose into the same transaction
yield* Effect.tx(Effect.gen(function*() {
yield* TxRef.set(ref1, 10)
yield* Effect.tx(TxRef.set(ref2, 20))
const sum = (yield* TxRef.get(ref1)) + (yield* TxRef.get(ref2))
console.log(`Transaction sum: ${sum}`)
}))
console.log(`Final ref1: ${yield* TxRef.get(ref1)}`) // 10
console.log(`Final ref2: ${yield* TxRef.get(ref2)}`) // 20
})
tx)