effect-io-ai

Package: effect
Module: Ref

Ref.updateSome

Updates the value of the Ref atomically using the given partial function.

When to use

Use to apply a conditional Ref update without returning a value.

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.

Example (Conditionally updating a value)

import { Effect, Option, Ref } from "effect"

const program = Effect.gen(function*() {
  const counter = yield* Ref.make(5)

  // Only update if value is even
  yield* Ref.updateSome(
    counter,
    (n) => n % 2 === 0 ? Option.some(n * 2) : Option.none()
  )

  let current = yield* Ref.get(counter)
  console.log(current) // 5 (unchanged because 5 is odd)

  // Set to even number and try again
  yield* Ref.set(counter, 6)
  yield* Ref.updateSome(
    counter,
    (n) => n % 2 === 0 ? Option.some(n * 2) : Option.none()
  )

  current = yield* Ref.get(counter)
  console.log(current) // 12 (updated because 6 is even)
})

See

Signature

declare const updateSome: (<A>(f: (a: A) => Option.Option<A>) => (self: Ref<A>) => Effect.Effect<void>) & (<A>(self: Ref<A>, f: (a: A) => Option.Option<A>) => Effect.Effect<void>)

Source

Since v2.0.0