Hyperlinkv0.8.0-beta.28

TxSubscriptionRef

TxSubscriptionRef.updateAndGetconsteffect/TxSubscriptionRef.ts:385
<A>(f: (current: A) => A): (
  self: TxSubscriptionRef<A>
) => Effect.Effect<A>
<A>(self: TxSubscriptionRef<A>, f: (current: A) => A): Effect.Effect<A>

Updates the value using a function and returns the new value. Publishes the new value to all subscribers.

When to use

Use to derive and publish a new TxSubscriptionRef value while returning that new value.

Example (Updating and reading atomically)

import { Effect, TxSubscriptionRef } from "effect"

const program = Effect.gen(function*() {
  const ref = yield* TxSubscriptionRef.make(3)
  const result = yield* TxSubscriptionRef.updateAndGet(ref, (n) => n * 3)
  console.log(result) // 9
})
export const updateAndGet: {
  <A>(f: (current: A) => A): (self: TxSubscriptionRef<A>) => Effect.Effect<A>
  <A>(self: TxSubscriptionRef<A>, f: (current: A) => A): Effect.Effect<A>
} = dual(
  2,
  <A>(self: TxSubscriptionRef<A>, f: (current: A) => A): Effect.Effect<A> =>
    modify(self, (current) => {
      const newValue = f(current)
      return [newValue, newValue]
    })
)