Hyperlinkv0.8.0-beta.28

TxSubscriptionRef

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

Gets the current value and updates it using a function atomically. Publishes the new value to all subscribers.

When to use

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

Example (Getting and updating atomically)

import { Effect, TxSubscriptionRef } from "effect"

const program = Effect.gen(function*() {
  const ref = yield* TxSubscriptionRef.make(1)
  const old = yield* TxSubscriptionRef.getAndUpdate(ref, (n) => n + 10)
  console.log(old) // 1
  console.log(yield* TxSubscriptionRef.get(ref)) // 11
})
export const getAndUpdate: {
  <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) => [current, f(current)])
)