<A>(initial: A): TxRef<A>Creates a new TxRef synchronously with the specified initial value.
When to use
Use to construct a TxRef synchronously when it must be created outside an
Effect workflow.
Example (Creating transactional references unsafely)
import { TxRef } from "effect"
// Create a TxRef synchronously (unsafe - use make instead in Effect contexts)
const counter = TxRef.makeUnsafe(0)
const config = TxRef.makeUnsafe({ timeout: 5000, retries: 3 })
// These are now ready to use in transactions
console.log(counter.value) // 0
console.log(config.value) // { timeout: 5000, retries: 3 }export const const makeUnsafe: <A>(
initial: A
) => TxRef<A>
Creates a new TxRef synchronously with the specified initial value.
When to use
Use to construct a TxRef synchronously when it must be created outside an
Effect workflow.
Example (Creating transactional references unsafely)
import { TxRef } from "effect"
// Create a TxRef synchronously (unsafe - use make instead in Effect contexts)
const counter = TxRef.makeUnsafe(0)
const config = TxRef.makeUnsafe({ timeout: 5000, retries: 3 })
// These are now ready to use in transactions
console.log(counter.value) // 0
console.log(config.value) // { timeout: 5000, retries: 3 }
makeUnsafe = <function (type parameter) A in <A>(initial: A): TxRef<A>A>(initial: Ainitial: function (type parameter) A in <A>(initial: A): TxRef<A>A): 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<function (type parameter) A in <A>(initial: A): TxRef<A>A> => ({
[const TypeId: "~effect/transactions/TxRef"TypeId]: const TypeId: "~effect/transactions/TxRef"TypeId,
TxRef<A>.pending: Map<unknown, () => void>pending: new var Map: MapConstructor
new () => Map<any, any> (+3 overloads)
Map(),
function pipe(): anypipe() {
return import pipeArgumentspipeArguments(this, function (local var) arguments: IArgumentsarguments)
},
TxRef<in out A>.version: numberversion: 0,
TxRef<A>.value: Avalue: initial: Ainitial
})