Hyperlinkv0.8.0-beta.28

SynchronizedRef

SynchronizedRef.getAndUpdateconsteffect/SynchronizedRef.ts:165
<A>(f: (a: A) => A): (self: SynchronizedRef<A>) => Effect.Effect<A>
<A>(self: SynchronizedRef<A>, f: (a: A) => A): Effect.Effect<A>

Updates the current value atomically with a function and returns the previous value, serialized by the ref's semaphore.

When to use

Use to run a pure SynchronizedRef state update when the previous stored value is also needed.

export const getAndUpdate: {
  <A>(f: (a: A) => A): (self: SynchronizedRef<A>) => Effect.Effect<A>
  <A>(self: SynchronizedRef<A>, f: (a: A) => A): Effect.Effect<A>
} = dual(
  2,
  <A>(self: SynchronizedRef<A>, f: (a: A) => A): Effect.Effect<A> =>
    self.semaphore.withPermit(Ref.getAndUpdate(self.backing, f))
)