<A>(f: (current: NoInfer<A>) => A): (
self: TxRef<A>
) => Effect.Effect<void>
<A>(self: TxRef<A>, f: (current: A) => A): Effect.Effect<void>Updates the value of the TxRef using the provided function.
When to use
Use to transform a TxRef when no result value is needed.
Example (Updating transactional references)
import { Effect, TxRef } from "effect"
const program = Effect.gen(function*() {
const counter = yield* TxRef.make(10)
// Update the value using a function
yield* Effect.tx(
TxRef.update(counter, (current) => current * 2)
)
console.log(yield* TxRef.get(counter)) // 20
})export const const update: {
<A>(f: (current: NoInfer<A>) => A): (
self: TxRef<A>
) => Effect.Effect<void>
<A>(
self: TxRef<A>,
f: (current: A) => A
): Effect.Effect<void>
}
Updates the value of the TxRef using the provided function.
When to use
Use to transform a TxRef when no result value is needed.
Example (Updating transactional references)
import { Effect, TxRef } from "effect"
const program = Effect.gen(function*() {
const counter = yield* TxRef.make(10)
// Update the value using a function
yield* Effect.tx(
TxRef.update(counter, (current) => current * 2)
)
console.log(yield* TxRef.get(counter)) // 20
})
update: {
<function (type parameter) A in <A>(f: (current: NoInfer<A>) => A): (self: TxRef<A>) => Effect.Effect<void>A>(f: (current: NoInfer<A>) => Af: (current: NoInfer<A>current: type NoInfer<A> = [A][A extends any ? 0 : never]Prevents TypeScript from inferring a type parameter from a specific
position.
When to use
Use when a function parameter must match an inferred type without becoming
an inference source.
Details
The parameter using NoInfer must still match the inferred type.
Example (Controlling inference)
import type { Types } from "effect"
declare function withDefault<T>(value: T, fallback: Types.NoInfer<T>): T
// T is inferred as "a" | "b" from the first argument only
const result = withDefault<"a" | "b">("a", "b")
NoInfer<function (type parameter) A in <A>(f: (current: NoInfer<A>) => A): (self: TxRef<A>) => Effect.Effect<void>A>) => function (type parameter) A in <A>(f: (current: NoInfer<A>) => A): (self: TxRef<A>) => Effect.Effect<void>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>(f: (current: NoInfer<A>) => A): (self: TxRef<A>) => Effect.Effect<void>A>) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<void>
<function (type parameter) A in <A>(self: TxRef<A>, f: (current: A) => A): Effect.Effect<void>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>, f: (current: A) => A): Effect.Effect<void>A>, f: (current: A) => Af: (current: Acurrent: function (type parameter) A in <A>(self: TxRef<A>, f: (current: A) => A): Effect.Effect<void>A) => function (type parameter) A in <A>(self: TxRef<A>, f: (current: A) => A): Effect.Effect<void>A): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<void>
} = import dualdual(2, <function (type parameter) A in <A>(self: TxRef<A>, f: (current: A) => A): Effect.Effect<void>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>, f: (current: A) => A): Effect.Effect<void>A>,
f: (current: A) => Af: (current: Acurrent: function (type parameter) A in <A>(self: TxRef<A>, f: (current: A) => A): Effect.Effect<void>A) => function (type parameter) A in <A>(self: TxRef<A>, f: (current: A) => A): Effect.Effect<void>A
): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<void> => 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) => [void 0, f: (current: A) => Af(current: Acurrent)]))