<A>(iterable: Iterable<A>): Effect.Effect<TxChunk<A>>Creates a new TxChunk from an iterable.
Details
This function returns a new TxChunk reference containing elements from the provided iterable. No existing TxChunk instances are modified.
Example (Creating from an iterable)
import { Chunk, Effect, TxChunk } from "effect"
const program = Effect.gen(function*() {
// Create TxChunk from array
const txChunk = yield* TxChunk.fromIterable([1, 2, 3, 4, 5])
// Read the contents - automatically transactional
const chunk = yield* TxChunk.get(txChunk)
console.log(Chunk.toReadonlyArray(chunk)) // [1, 2, 3, 4, 5]
// Multi-step atomic modification - use explicit transaction
yield* Effect.tx(
Effect.gen(function*() {
yield* TxChunk.append(txChunk, 6)
yield* TxChunk.prepend(txChunk, 0)
})
)
const updated = yield* TxChunk.get(txChunk)
console.log(Chunk.toReadonlyArray(updated)) // [0, 1, 2, 3, 4, 5, 6]
})export const const fromIterable: <A>(
iterable: Iterable<A>
) => Effect.Effect<TxChunk<A>>
Creates a new TxChunk from an iterable.
Details
This function returns a new TxChunk reference containing elements from the provided iterable. No
existing TxChunk instances are modified.
Example (Creating from an iterable)
import { Chunk, Effect, TxChunk } from "effect"
const program = Effect.gen(function*() {
// Create TxChunk from array
const txChunk = yield* TxChunk.fromIterable([1, 2, 3, 4, 5])
// Read the contents - automatically transactional
const chunk = yield* TxChunk.get(txChunk)
console.log(Chunk.toReadonlyArray(chunk)) // [1, 2, 3, 4, 5]
// Multi-step atomic modification - use explicit transaction
yield* Effect.tx(
Effect.gen(function*() {
yield* TxChunk.append(txChunk, 6)
yield* TxChunk.prepend(txChunk, 0)
})
)
const updated = yield* TxChunk.get(txChunk)
console.log(Chunk.toReadonlyArray(updated)) // [0, 1, 2, 3, 4, 5, 6]
})
fromIterable = <function (type parameter) A in <A>(iterable: Iterable<A>): Effect.Effect<TxChunk<A>>A>(iterable: Iterable<A>iterable: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(iterable: Iterable<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>(iterable: Iterable<A>): Effect.Effect<TxChunk<A>>A>> =>
import EffectEffect.map(import TxRefTxRef.const make: <any>(initial: any) => 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(import ChunkChunk.fromIterable(iterable: Iterable<A>iterable)), (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))