<A>(pf: (a: A) => Option.Option<A>): (self: Ref<A>) => Effect.Effect<A>
<A>(self: Ref<A>, pf: (a: A) => Option.Option<A>): Effect.Effect<A>Gets the current value of the Ref and updates it atomically with the given partial function.
When to use
Use to return the previous Ref value while applying a conditional update.
Details
If the partial function returns Option.some, the Ref is updated with the
new value. If it returns Option.none, the Ref is left unchanged. The effect
always returns the value that was in the Ref before the attempted update.
Example (Conditionally updating and returning the previous value)
import { Effect, Option, Ref } from "effect"
const program = Effect.gen(function*() {
const counter = yield* Ref.make(5)
// Only update if value is greater than 3
const previous1 = yield* Ref.getAndUpdateSome(
counter,
(n) => n > 3 ? Option.some(n * 2) : Option.none()
)
console.log(previous1) // 5
const current1 = yield* Ref.get(counter)
console.log(current1) // 10
// Try to update again (won't update since 10 > 3 is true but let's say condition is n < 3)
const previous2 = yield* Ref.getAndUpdateSome(
counter,
(n) => n < 3 ? Option.some(n * 2) : Option.none()
)
console.log(previous2) // 10
const current2 = yield* Ref.get(counter)
console.log(current2) // 10 (unchanged)
})export const const getAndUpdateSome: (<A>(
pf: (a: A) => Option.Option<A>
) => (self: Ref<A>) => Effect.Effect<A>) &
(<A>(
self: Ref<A>,
pf: (a: A) => Option.Option<A>
) => Effect.Effect<A>)
Gets the current value of the Ref and updates it atomically with the given partial function.
When to use
Use to return the previous Ref value while applying a conditional update.
Details
If the partial function returns Option.some, the Ref is updated with the
new value. If it returns Option.none, the Ref is left unchanged. The effect
always returns the value that was in the Ref before the attempted update.
Example (Conditionally updating and returning the previous value)
import { Effect, Option, Ref } from "effect"
const program = Effect.gen(function*() {
const counter = yield* Ref.make(5)
// Only update if value is greater than 3
const previous1 = yield* Ref.getAndUpdateSome(
counter,
(n) => n > 3 ? Option.some(n * 2) : Option.none()
)
console.log(previous1) // 5
const current1 = yield* Ref.get(counter)
console.log(current1) // 10
// Try to update again (won't update since 10 > 3 is true but let's say condition is n < 3)
const previous2 = yield* Ref.getAndUpdateSome(
counter,
(n) => n < 3 ? Option.some(n * 2) : Option.none()
)
console.log(previous2) // 10
const current2 = yield* Ref.get(counter)
console.log(current2) // 10 (unchanged)
})
getAndUpdateSome = import dualdual<
<function (type parameter) A in <A>(pf: (a: A) => Option.Option<A>): (self: Ref<A>) => Effect.Effect<A>A>(pf: (a: A) => Option.Option<A>pf: (a: Aa: function (type parameter) A in <A>(pf: (a: A) => Option.Option<A>): (self: Ref<A>) => Effect.Effect<A>A) => import OptionOption.type Option.Option = /*unresolved*/ anyOption<function (type parameter) A in <A>(pf: (a: A) => Option.Option<A>): (self: Ref<A>) => Effect.Effect<A>A>) => (self: Ref<A>(parameter) self: {
ref: MutableRef.MutableRef<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 Ref<in out A>A mutable reference that provides atomic read, write, and update operations.
When to use
Use to keep shared mutable state that is read and updated inside Effect
programs.
Details
A Ref is a thread-safe mutable reference type for shared state. It supports
simple read and write operations as well as atomic transformations.
Example (Reading and updating a ref)
import { Effect, Ref } from "effect"
const program = Effect.gen(function*() {
// Create a ref with initial value
const counter = yield* Ref.make(0)
// Read the current value
const value = yield* Ref.get(counter)
console.log(value) // 0
// Update the value atomically
yield* Ref.update(counter, (n) => n + 1)
// Read the updated value
const newValue = yield* Ref.get(counter)
console.log(newValue) // 1
})
The Ref namespace containing type definitions and utilities.
When to use
Use when referring to type members nested under the Ref namespace.
Ref<function (type parameter) A in <A>(pf: (a: A) => Option.Option<A>): (self: Ref<A>) => Effect.Effect<A>A>) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <A>(pf: (a: A) => Option.Option<A>): (self: Ref<A>) => Effect.Effect<A>A>,
<function (type parameter) A in <A>(self: Ref<A>, pf: (a: A) => Option.Option<A>): Effect.Effect<A>A>(self: Ref<A>(parameter) self: {
ref: MutableRef.MutableRef<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 Ref<in out A>A mutable reference that provides atomic read, write, and update operations.
When to use
Use to keep shared mutable state that is read and updated inside Effect
programs.
Details
A Ref is a thread-safe mutable reference type for shared state. It supports
simple read and write operations as well as atomic transformations.
Example (Reading and updating a ref)
import { Effect, Ref } from "effect"
const program = Effect.gen(function*() {
// Create a ref with initial value
const counter = yield* Ref.make(0)
// Read the current value
const value = yield* Ref.get(counter)
console.log(value) // 0
// Update the value atomically
yield* Ref.update(counter, (n) => n + 1)
// Read the updated value
const newValue = yield* Ref.get(counter)
console.log(newValue) // 1
})
The Ref namespace containing type definitions and utilities.
When to use
Use when referring to type members nested under the Ref namespace.
Ref<function (type parameter) A in <A>(self: Ref<A>, pf: (a: A) => Option.Option<A>): Effect.Effect<A>A>, pf: (a: A) => Option.Option<A>pf: (a: Aa: function (type parameter) A in <A>(self: Ref<A>, pf: (a: A) => Option.Option<A>): Effect.Effect<A>A) => import OptionOption.type Option.Option = /*unresolved*/ anyOption<function (type parameter) A in <A>(self: Ref<A>, pf: (a: A) => Option.Option<A>): Effect.Effect<A>A>) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <A>(self: Ref<A>, pf: (a: A) => Option.Option<A>): Effect.Effect<A>A>
>(2, <function (type parameter) A in <A>(self: Ref<A>, pf: (a: A) => Option.Option<A>): anyA>(self: Ref<A>(parameter) self: {
ref: MutableRef.MutableRef<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 Ref<in out A>A mutable reference that provides atomic read, write, and update operations.
When to use
Use to keep shared mutable state that is read and updated inside Effect
programs.
Details
A Ref is a thread-safe mutable reference type for shared state. It supports
simple read and write operations as well as atomic transformations.
Example (Reading and updating a ref)
import { Effect, Ref } from "effect"
const program = Effect.gen(function*() {
// Create a ref with initial value
const counter = yield* Ref.make(0)
// Read the current value
const value = yield* Ref.get(counter)
console.log(value) // 0
// Update the value atomically
yield* Ref.update(counter, (n) => n + 1)
// Read the updated value
const newValue = yield* Ref.get(counter)
console.log(newValue) // 1
})
The Ref namespace containing type definitions and utilities.
When to use
Use when referring to type members nested under the Ref namespace.
Ref<function (type parameter) A in <A>(self: Ref<A>, pf: (a: A) => Option.Option<A>): anyA>, pf: (a: A) => Option.Option<A>pf: (a: Aa: function (type parameter) A in <A>(self: Ref<A>, pf: (a: A) => Option.Option<A>): anyA) => import OptionOption.type Option.Option = /*unresolved*/ anyOption<function (type parameter) A in <A>(self: Ref<A>, pf: (a: A) => Option.Option<A>): anyA>) =>
import EffectEffect.sync(() => {
const const current: anycurrent = self: Ref<A>(parameter) self: {
ref: MutableRef.MutableRef<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.Ref<in out A>.ref: MutableRef.MutableRef<A>(property) Ref<in out A>.ref: {
current: T;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
ref.current
const const option: Option.Option<A>option = pf: (a: A) => Option.Option<A>pf(const current: anycurrent)
if (const option: Option.Option<A>option._tag === "Some") {
self: Ref<A>(parameter) self: {
ref: MutableRef.MutableRef<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.Ref<in out A>.ref: MutableRef.MutableRef<A>(property) Ref<in out A>.ref: {
current: T;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
ref.current = const option: Option.Some<A>const option: {
_tag: "Some";
_op: "Some";
value: A;
valueOrUndefined: 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; <…;
toString: () => string;
toJSON: () => unknown;
}
option.value
}
return const current: anycurrent
}))