<A>(self: TxRef<A>): Effect.Effect<A>Reads the current value of the TxRef.
When to use
Use to read the current value of a TxRef.
Example (Reading transactional references)
import { Effect, TxRef } from "effect"
const program = Effect.gen(function*() {
const counter = yield* TxRef.make(42)
// Read the value within a transaction
const value = yield* Effect.tx(
TxRef.get(counter)
)
console.log(value) // 42
})export const const get: <A>(
self: TxRef<A>
) => Effect.Effect<A>
Reads the current value of the TxRef.
When to use
Use to read the current value of a TxRef.
Example (Reading transactional references)
import { Effect, TxRef } from "effect"
const program = Effect.gen(function*() {
const counter = yield* TxRef.make(42)
// Read the value within a transaction
const value = yield* Effect.tx(
TxRef.get(counter)
)
console.log(value) // 42
})
get = <function (type parameter) A in <A>(self: TxRef<A>): Effect.Effect<A>A>(self: TxRef<A>(parameter) self: {
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; <…;
}
self: 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>(self: TxRef<A>): Effect.Effect<A>A>): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <A>(self: TxRef<A>): Effect.Effect<A>A> => const modify: {
<A, R>(
f: (
current: NoInfer<A>
) => [returnValue: R, newValue: A]
): (self: TxRef<A>) => Effect.Effect<R>
<A, R>(
self: TxRef<A>,
f: (
current: A
) => [returnValue: R, newValue: A]
): Effect.Effect<R>
}
modify(self: TxRef<A>(parameter) self: {
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; <…;
}
self, (current: Acurrent) => [current: Acurrent, current: Acurrent])