<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)export const 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 = <function (type parameter) A in <A>(ref: TxRef.TxRef<Chunk.Chunk<A>>): TxChunk<A>A>(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: import TxRefTxRef.interface TxRef<in out A>TxRef is a transactional value, it can be read and modified within the body of a transaction.
When to use
Use to store mutable state that must be read and modified inside Effect
transactions.
Details
Accessed values are tracked by the transaction in order to detect conflicts and in order to
track changes, a transaction will retry whenever a conflict is detected or whenever the
transaction explicitely calls to Effect.txRetry and any of the accessed TxRef values
change.
Example (Using a transactional reference)
import { Effect, TxRef } from "effect"
const program = Effect.gen(function*() {
// Create a transactional reference
const ref: TxRef.TxRef<number> = yield* TxRef.make(0)
// Use within a transaction
yield* Effect.tx(Effect.gen(function*() {
const current = yield* TxRef.get(ref)
yield* TxRef.set(ref, current + 1)
}))
const final = yield* TxRef.get(ref)
console.log(final) // 1
})
TxRef<import ChunkChunk.type Chunk.Chunk = /*unresolved*/ anyChunk<function (type parameter) A in <A>(ref: TxRef.TxRef<Chunk.Chunk<A>>): TxChunk<A>A>>): 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>(ref: TxRef.TxRef<Chunk.Chunk<A>>): TxChunk<A>A> => {
const const txChunk: anytxChunk = 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 TxChunkProto: {
[NodeInspectSymbol](
this: TxChunk<unknown>
): unknown
toString(this: TxChunk<unknown>): string
toJSON(this: TxChunk<unknown>): {
_id: string
ref: unknown
}
pipe(this: TxChunk<unknown>): unknown
}
TxChunkProto)
const txChunk: anytxChunk[const TypeId: "~effect/transactions/TxChunk"TypeId] = const TypeId: "~effect/transactions/TxChunk"TypeId
const txChunk: anytxChunk.ref = 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
return const txChunk: anytxChunk
}