Hyperlinkv0.8.0-beta.28

SubscriptionRef

SubscriptionRef.updateAndGetconsteffect/SubscriptionRef.ts:783
<A>(update: (a: A) => A): (self: SubscriptionRef<A>) => Effect.Effect<A>
<A>(self: SubscriptionRef<A>, update: (a: A) => A): Effect.Effect<A>

Updates the value of the SubscriptionRef with the result of applying a function and returns the new value, notifying subscribers of the change.

Example (Updating and reading the new value)

import { Effect, SubscriptionRef } from "effect"

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

  const newValue = yield* SubscriptionRef.updateAndGet(ref, (n) => n * 2)
  console.log("New value:", newValue)
})
updating
export const updateAndGet: {
  <A>(update: (a: A) => A): (self: SubscriptionRef<A>) => Effect.Effect<A>
  <A>(self: SubscriptionRef<A>, update: (a: A) => A): Effect.Effect<A>
} = dual(2, <A>(self: SubscriptionRef<A>, update: (a: A) => A) =>
  self.semaphore.withPermit(Effect.sync(() => {
    const newValue = update(self.value)
    setUnsafe(self, newValue)
    return newValue
  })))