Hyperlinkv0.8.0-beta.28

Ref

Ref.updateSomeAndGetconsteffect/Ref.ts:758
<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>

Updates the value of the Ref atomically using the given partial function and returns the current value.

When to use

Use to apply a conditional Ref update and return the resulting current 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. The effect returns the current value of the Ref after the potential update.

Example (Conditionally updating and returning the current value)

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

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

  // Only update if value is greater than 5
  const result1 = yield* Ref.updateSomeAndGet(
    counter,
    (n) => n > 5 ? Option.some(n / 2) : Option.none()
  )
  console.log(result1) // 5 (updated and returned)

  // Try to update again with same condition
  const result2 = yield* Ref.updateSomeAndGet(
    counter,
    (n) => n > 5 ? Option.some(n / 2) : Option.none()
  )
  console.log(result2) // 5 (unchanged because 5 is not > 5)
})
Source effect/Ref.ts:75811 lines
export const updateSomeAndGet = dual<
  <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>
>(2, <A>(self: Ref<A>, pf: (a: A) => Option.Option<A>) =>
  Effect.sync(() => {
    const option = pf(self.ref.current)
    if (option._tag === "Some") {
      self.ref.current = option.value
    }
    return self.ref.current
  }))
Referenced by 1 symbols