Hyperlinkv0.8.0-beta.28

Ref

Ref.setAndGetconsteffect/Ref.ts:432
<A>(value: A): (self: Ref<A>) => Effect.Effect<A>
<A>(self: Ref<A>, value: A): Effect.Effect<A>

Sets the value of the Ref atomically to the specified value and returns the new value.

When to use

Use when you want to set a Ref value and immediately get it back in one atomic operation.

Example (Setting and returning the new value)

import { Effect, Ref } from "effect"

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

  // Set new value and get it back in one operation
  const newValue = yield* Ref.setAndGet(ref, 42)
  console.log(newValue) // 42

  // Verify the ref contains the new value
  const current = yield* Ref.get(ref)
  console.log(current) // 42
})

// Useful for sequential operations
const program2 = Effect.gen(function*() {
  const counter = yield* Ref.make(0)

  const newValue = yield* Ref.setAndGet(counter, 20)
  console.log(newValue) // 20
})
mutations
Source effect/Ref.ts:4324 lines
export const setAndGet = dual<
  <A>(value: A) => (self: Ref<A>) => Effect.Effect<A>,
  <A>(self: Ref<A>, value: A) => Effect.Effect<A>
>(2, <A>(self: Ref<A>, value: A) => Effect.sync(() => self.ref.current = value))
Referenced by 1 symbols