<T>(f: (value: T) => T): (self: MutableRef<T>) => MutableRef<T>
<T>(self: MutableRef<T>, f: (value: T) => T): MutableRef<T>Updates the MutableRef with the result of applying a function to its current value, and returns the reference.
When to use
Use when you need an in-place MutableRef value transformation that returns
the same MutableRef.
Example (Updating values)
import { MutableRef } from "effect"
const counter = MutableRef.make(5)
// Increment the counter
MutableRef.update(counter, (n) => n + 1)
console.log(MutableRef.get(counter)) // 6
// Chain updates (since it returns the ref)
const result = MutableRef.update(counter, (n) => n * 2)
console.log(result === counter) // true (same reference)
console.log(MutableRef.get(counter)) // 12
// Transform string
const message = MutableRef.make("hello")
MutableRef.update(message, (s) => s.toUpperCase())
console.log(MutableRef.get(message)) // "HELLO"
// Update complex objects
const user = MutableRef.make({ name: "Alice", age: 30 })
MutableRef.update(user, (u) => ({ ...u, age: u.age + 1 }))
console.log(MutableRef.get(user)) // { name: "Alice", age: 31 }
// Pipe-able version
const double = MutableRef.update((n: number) => n * 2)
double(counter)
console.log(MutableRef.get(counter)) // 24
// Array operations
const list = MutableRef.make<Array<number>>([1, 2, 3])
MutableRef.update(list, (arr) => [...arr, 4])
console.log(MutableRef.get(list)) // [1, 2, 3, 4]export const const update: {
<T>(f: (value: T) => T): (
self: MutableRef<T>
) => MutableRef<T>
<T>(
self: MutableRef<T>,
f: (value: T) => T
): MutableRef<T>
}
Updates the MutableRef with the result of applying a function to its current value,
and returns the reference.
When to use
Use when you need an in-place MutableRef value transformation that returns
the same MutableRef.
Example (Updating values)
import { MutableRef } from "effect"
const counter = MutableRef.make(5)
// Increment the counter
MutableRef.update(counter, (n) => n + 1)
console.log(MutableRef.get(counter)) // 6
// Chain updates (since it returns the ref)
const result = MutableRef.update(counter, (n) => n * 2)
console.log(result === counter) // true (same reference)
console.log(MutableRef.get(counter)) // 12
// Transform string
const message = MutableRef.make("hello")
MutableRef.update(message, (s) => s.toUpperCase())
console.log(MutableRef.get(message)) // "HELLO"
// Update complex objects
const user = MutableRef.make({ name: "Alice", age: 30 })
MutableRef.update(user, (u) => ({ ...u, age: u.age + 1 }))
console.log(MutableRef.get(user)) // { name: "Alice", age: 31 }
// Pipe-able version
const double = MutableRef.update((n: number) => n * 2)
double(counter)
console.log(MutableRef.get(counter)) // 24
// Array operations
const list = MutableRef.make<Array<number>>([1, 2, 3])
MutableRef.update(list, (arr) => [...arr, 4])
console.log(MutableRef.get(list)) // [1, 2, 3, 4]
update: {
<function (type parameter) T in <T>(f: (value: T) => T): (self: MutableRef<T>) => MutableRef<T>T>(f: (value: T) => Tf: (value: Tvalue: function (type parameter) T in <T>(f: (value: T) => T): (self: MutableRef<T>) => MutableRef<T>T) => function (type parameter) T in <T>(f: (value: T) => T): (self: MutableRef<T>) => MutableRef<T>T): (self: MutableRef<T>(parameter) self: {
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;
}
self: interface MutableRef<out T>A synchronous mutable reference that stores a current value.
When to use
Use to keep local mutable state in a stable, pipeable reference.
Details
Read or write the value directly through .current, or use the MutableRef
helpers for pipeable updates such as get, set, update, and
compareAndSet. All operations mutate the same reference in place.
Example (Creating and updating refs)
import { MutableRef } from "effect"
// Create a mutable reference
const ref: MutableRef.MutableRef<number> = MutableRef.make(42)
// Read the current value
console.log(ref.current) // 42
console.log(MutableRef.get(ref)) // 42
// Update the value
ref.current = 100
console.log(MutableRef.get(ref)) // 100
// Use with complex types
interface Config {
timeout: number
retries: number
}
const config: MutableRef.MutableRef<Config> = MutableRef.make({
timeout: 5000,
retries: 3
})
// Update through the interface
config.current = { timeout: 10000, retries: 5 }
console.log(config.current.timeout) // 10000
MutableRef<function (type parameter) T in <T>(f: (value: T) => T): (self: MutableRef<T>) => MutableRef<T>T>) => interface MutableRef<out T>A synchronous mutable reference that stores a current value.
When to use
Use to keep local mutable state in a stable, pipeable reference.
Details
Read or write the value directly through .current, or use the MutableRef
helpers for pipeable updates such as get, set, update, and
compareAndSet. All operations mutate the same reference in place.
Example (Creating and updating refs)
import { MutableRef } from "effect"
// Create a mutable reference
const ref: MutableRef.MutableRef<number> = MutableRef.make(42)
// Read the current value
console.log(ref.current) // 42
console.log(MutableRef.get(ref)) // 42
// Update the value
ref.current = 100
console.log(MutableRef.get(ref)) // 100
// Use with complex types
interface Config {
timeout: number
retries: number
}
const config: MutableRef.MutableRef<Config> = MutableRef.make({
timeout: 5000,
retries: 3
})
// Update through the interface
config.current = { timeout: 10000, retries: 5 }
console.log(config.current.timeout) // 10000
MutableRef<function (type parameter) T in <T>(f: (value: T) => T): (self: MutableRef<T>) => MutableRef<T>T>
<function (type parameter) T in <T>(self: MutableRef<T>, f: (value: T) => T): MutableRef<T>T>(self: MutableRef<T>(parameter) self: {
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;
}
self: interface MutableRef<out T>A synchronous mutable reference that stores a current value.
When to use
Use to keep local mutable state in a stable, pipeable reference.
Details
Read or write the value directly through .current, or use the MutableRef
helpers for pipeable updates such as get, set, update, and
compareAndSet. All operations mutate the same reference in place.
Example (Creating and updating refs)
import { MutableRef } from "effect"
// Create a mutable reference
const ref: MutableRef.MutableRef<number> = MutableRef.make(42)
// Read the current value
console.log(ref.current) // 42
console.log(MutableRef.get(ref)) // 42
// Update the value
ref.current = 100
console.log(MutableRef.get(ref)) // 100
// Use with complex types
interface Config {
timeout: number
retries: number
}
const config: MutableRef.MutableRef<Config> = MutableRef.make({
timeout: 5000,
retries: 3
})
// Update through the interface
config.current = { timeout: 10000, retries: 5 }
console.log(config.current.timeout) // 10000
MutableRef<function (type parameter) T in <T>(self: MutableRef<T>, f: (value: T) => T): MutableRef<T>T>, f: (value: T) => Tf: (value: Tvalue: function (type parameter) T in <T>(self: MutableRef<T>, f: (value: T) => T): MutableRef<T>T) => function (type parameter) T in <T>(self: MutableRef<T>, f: (value: T) => T): MutableRef<T>T): interface MutableRef<out T>A synchronous mutable reference that stores a current value.
When to use
Use to keep local mutable state in a stable, pipeable reference.
Details
Read or write the value directly through .current, or use the MutableRef
helpers for pipeable updates such as get, set, update, and
compareAndSet. All operations mutate the same reference in place.
Example (Creating and updating refs)
import { MutableRef } from "effect"
// Create a mutable reference
const ref: MutableRef.MutableRef<number> = MutableRef.make(42)
// Read the current value
console.log(ref.current) // 42
console.log(MutableRef.get(ref)) // 42
// Update the value
ref.current = 100
console.log(MutableRef.get(ref)) // 100
// Use with complex types
interface Config {
timeout: number
retries: number
}
const config: MutableRef.MutableRef<Config> = MutableRef.make({
timeout: 5000,
retries: 3
})
// Update through the interface
config.current = { timeout: 10000, retries: 5 }
console.log(config.current.timeout) // 10000
MutableRef<function (type parameter) T in <T>(self: MutableRef<T>, f: (value: T) => T): MutableRef<T>T>
} = import DualDual.dual<
<function (type parameter) T in <T>(f: (value: T) => T): (self: MutableRef<T>) => MutableRef<T>T>(f: (value: T) => Tf: (value: Tvalue: function (type parameter) T in <T>(f: (value: T) => T): (self: MutableRef<T>) => MutableRef<T>T) => function (type parameter) T in <T>(f: (value: T) => T): (self: MutableRef<T>) => MutableRef<T>T) => (self: MutableRef<T>(parameter) self: {
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;
}
self: interface MutableRef<out T>A synchronous mutable reference that stores a current value.
When to use
Use to keep local mutable state in a stable, pipeable reference.
Details
Read or write the value directly through .current, or use the MutableRef
helpers for pipeable updates such as get, set, update, and
compareAndSet. All operations mutate the same reference in place.
Example (Creating and updating refs)
import { MutableRef } from "effect"
// Create a mutable reference
const ref: MutableRef.MutableRef<number> = MutableRef.make(42)
// Read the current value
console.log(ref.current) // 42
console.log(MutableRef.get(ref)) // 42
// Update the value
ref.current = 100
console.log(MutableRef.get(ref)) // 100
// Use with complex types
interface Config {
timeout: number
retries: number
}
const config: MutableRef.MutableRef<Config> = MutableRef.make({
timeout: 5000,
retries: 3
})
// Update through the interface
config.current = { timeout: 10000, retries: 5 }
console.log(config.current.timeout) // 10000
MutableRef<function (type parameter) T in <T>(f: (value: T) => T): (self: MutableRef<T>) => MutableRef<T>T>) => interface MutableRef<out T>A synchronous mutable reference that stores a current value.
When to use
Use to keep local mutable state in a stable, pipeable reference.
Details
Read or write the value directly through .current, or use the MutableRef
helpers for pipeable updates such as get, set, update, and
compareAndSet. All operations mutate the same reference in place.
Example (Creating and updating refs)
import { MutableRef } from "effect"
// Create a mutable reference
const ref: MutableRef.MutableRef<number> = MutableRef.make(42)
// Read the current value
console.log(ref.current) // 42
console.log(MutableRef.get(ref)) // 42
// Update the value
ref.current = 100
console.log(MutableRef.get(ref)) // 100
// Use with complex types
interface Config {
timeout: number
retries: number
}
const config: MutableRef.MutableRef<Config> = MutableRef.make({
timeout: 5000,
retries: 3
})
// Update through the interface
config.current = { timeout: 10000, retries: 5 }
console.log(config.current.timeout) // 10000
MutableRef<function (type parameter) T in <T>(f: (value: T) => T): (self: MutableRef<T>) => MutableRef<T>T>,
<function (type parameter) T in <T>(self: MutableRef<T>, f: (value: T) => T): MutableRef<T>T>(self: MutableRef<T>(parameter) self: {
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;
}
self: interface MutableRef<out T>A synchronous mutable reference that stores a current value.
When to use
Use to keep local mutable state in a stable, pipeable reference.
Details
Read or write the value directly through .current, or use the MutableRef
helpers for pipeable updates such as get, set, update, and
compareAndSet. All operations mutate the same reference in place.
Example (Creating and updating refs)
import { MutableRef } from "effect"
// Create a mutable reference
const ref: MutableRef.MutableRef<number> = MutableRef.make(42)
// Read the current value
console.log(ref.current) // 42
console.log(MutableRef.get(ref)) // 42
// Update the value
ref.current = 100
console.log(MutableRef.get(ref)) // 100
// Use with complex types
interface Config {
timeout: number
retries: number
}
const config: MutableRef.MutableRef<Config> = MutableRef.make({
timeout: 5000,
retries: 3
})
// Update through the interface
config.current = { timeout: 10000, retries: 5 }
console.log(config.current.timeout) // 10000
MutableRef<function (type parameter) T in <T>(self: MutableRef<T>, f: (value: T) => T): MutableRef<T>T>, f: (value: T) => Tf: (value: Tvalue: function (type parameter) T in <T>(self: MutableRef<T>, f: (value: T) => T): MutableRef<T>T) => function (type parameter) T in <T>(self: MutableRef<T>, f: (value: T) => T): MutableRef<T>T) => interface MutableRef<out T>A synchronous mutable reference that stores a current value.
When to use
Use to keep local mutable state in a stable, pipeable reference.
Details
Read or write the value directly through .current, or use the MutableRef
helpers for pipeable updates such as get, set, update, and
compareAndSet. All operations mutate the same reference in place.
Example (Creating and updating refs)
import { MutableRef } from "effect"
// Create a mutable reference
const ref: MutableRef.MutableRef<number> = MutableRef.make(42)
// Read the current value
console.log(ref.current) // 42
console.log(MutableRef.get(ref)) // 42
// Update the value
ref.current = 100
console.log(MutableRef.get(ref)) // 100
// Use with complex types
interface Config {
timeout: number
retries: number
}
const config: MutableRef.MutableRef<Config> = MutableRef.make({
timeout: 5000,
retries: 3
})
// Update through the interface
config.current = { timeout: 10000, retries: 5 }
console.log(config.current.timeout) // 10000
MutableRef<function (type parameter) T in <T>(self: MutableRef<T>, f: (value: T) => T): MutableRef<T>T>
>(2, (self: MutableRef<T>(parameter) self: {
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;
}
self, f: anyf) => const set: {
<T>(value: T): (
self: MutableRef<T>
) => MutableRef<T>
<T>(
self: MutableRef<T>,
value: T
): MutableRef<T>
}
set(self: MutableRef<T>(parameter) self: {
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;
}
self, f: anyf(const get: <T>(self: MutableRef<T>) => TGets the current value of the MutableRef.
When to use
Use to read the current MutableRef value without mutating it.
Example (Reading current values)
import { MutableRef } from "effect"
const ref = MutableRef.make("hello")
console.log(MutableRef.get(ref)) // "hello"
MutableRef.set(ref, "world")
console.log(MutableRef.get(ref)) // "world"
// Reading complex objects
const config = MutableRef.make({ port: 3000, host: "localhost" })
const currentConfig = MutableRef.get(config)
console.log(currentConfig.port) // 3000
// Multiple reads return the same value
const value1 = MutableRef.get(ref)
const value2 = MutableRef.get(ref)
console.log(value1 === value2) // true
get(self: MutableRef<T>(parameter) self: {
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;
}
self))))