<A>(initial: Chunk.Chunk<A>): Effect.Effect<TxChunk<A>>Creates a new TxChunk with the specified initial chunk.
Details
This function returns a new TxChunk reference containing the provided initial chunk. No existing TxChunk instances are modified.
Example (Creating a TxChunk from a chunk)
import { Chunk, Effect, TxChunk } from "effect"
const program = Effect.gen(function*() {
// Create a TxChunk with initial values
const initialChunk = Chunk.fromIterable([1, 2, 3])
const txChunk = yield* TxChunk.make(initialChunk)
// Read the value - automatically transactional
const result = yield* TxChunk.get(txChunk)
console.log(Chunk.toReadonlyArray(result)) // [1, 2, 3]
})export const const make: <A>(
initial: Chunk.Chunk<A>
) => Effect.Effect<TxChunk<A>>
Creates a new TxChunk with the specified initial chunk.
Details
This function returns a new TxChunk reference containing the provided initial chunk. No existing
TxChunk instances are modified.
Example (Creating a TxChunk from a chunk)
import { Chunk, Effect, TxChunk } from "effect"
const program = Effect.gen(function*() {
// Create a TxChunk with initial values
const initialChunk = Chunk.fromIterable([1, 2, 3])
const txChunk = yield* TxChunk.make(initialChunk)
// Read the value - automatically transactional
const result = yield* TxChunk.get(txChunk)
console.log(Chunk.toReadonlyArray(result)) // [1, 2, 3]
})
make = <function (type parameter) A in <A>(initial: Chunk.Chunk<A>): Effect.Effect<TxChunk<A>>A>(initial: Chunk.Chunk<A>(parameter) initial: {
length: number;
right: Chunk<A>;
left: Chunk<A>;
backing: Backing<A>;
depth: number;
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;
}
initial: import ChunkChunk.type Chunk.Chunk = /*unresolved*/ anyChunk<function (type parameter) A in <A>(initial: Chunk.Chunk<A>): Effect.Effect<TxChunk<A>>A>): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<interface TxChunk<in out A>TxChunk is a transactional chunk data structure that provides Software Transactional Memory (STM)
semantics for chunk operations.
Details
Accessed values are tracked by the transaction in order to detect conflicts and to track changes.
A transaction will retry whenever a conflict is detected or whenever the transaction explicitly
calls Effect.txRetry and any of the accessed TxChunk values change.
Example (Using a transactional chunk)
import { Chunk, Effect, TxChunk } from "effect"
const program = Effect.gen(function*() {
// Create a transactional chunk
const txChunk: TxChunk.TxChunk<number> = yield* TxChunk.fromIterable([
1,
2,
3
])
// Single operations - no explicit transaction needed
yield* TxChunk.append(txChunk, 4)
const result = yield* TxChunk.get(txChunk)
console.log(Chunk.toReadonlyArray(result)) // [1, 2, 3, 4]
// Multi-step atomic operation - use explicit transaction
yield* Effect.tx(
Effect.gen(function*() {
yield* TxChunk.prepend(txChunk, 0)
yield* TxChunk.append(txChunk, 5)
})
)
const finalResult = yield* TxChunk.get(txChunk)
console.log(Chunk.toReadonlyArray(finalResult)) // [0, 1, 2, 3, 4, 5]
})
TxChunk<function (type parameter) A in <A>(initial: Chunk.Chunk<A>): Effect.Effect<TxChunk<A>>A>> =>
import EffectEffect.map(import TxRefTxRef.const make: <Chunk.Chunk<A>>(initial: Chunk.Chunk<A>) => anyCreates 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(initial: Chunk.Chunk<A>(parameter) initial: {
length: number;
right: Chunk<A>;
left: Chunk<A>;
backing: Backing<A>;
depth: number;
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;
}
initial), (ref: TxRef.TxRef<Chunk.Chunk<A>>(parameter) ref: {
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; <…;
}
ref) => const makeUnsafe: <A>(
ref: TxRef.TxRef<Chunk.Chunk<A>>
) => TxChunk<A>
Creates a new TxChunk with the specified TxRef.
Details
This function returns a new TxChunk reference wrapping the provided TxRef. No existing TxChunk
instances are modified.
Example (Wrapping an existing TxRef)
import { Chunk, TxChunk, TxRef } from "effect"
// Create a TxChunk from an existing TxRef (advanced usage)
const ref = TxRef.makeUnsafe(Chunk.fromIterable([1, 2, 3]))
const txChunk = TxChunk.makeUnsafe(ref)
makeUnsafe(ref: TxRef.TxRef<Chunk.Chunk<A>>(parameter) ref: {
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; <…;
}
ref))