Hyperlinkv0.8.0-beta.28

SynchronizedRef

SynchronizedRef.getAndUpdateEffectconsteffect/SynchronizedRef.ts:192
<A, R, E>(f: (a: A) => Effect.Effect<A, E, R>): (
  self: SynchronizedRef<A>
) => Effect.Effect<A, E, R>
<A, R, E>(
  self: SynchronizedRef<A>,
  f: (a: A) => Effect.Effect<A, E, R>
): Effect.Effect<A, E, R>

Runs an effectful update atomically while holding the ref's semaphore, sets the new value if the effect succeeds, and returns the previous value.

When to use

Use when you need an effectful SynchronizedRef state transition to return the previous stored value.

export const getAndUpdateEffect: {
  <A, R, E>(f: (a: A) => Effect.Effect<A, E, R>): (self: SynchronizedRef<A>) => Effect.Effect<A, E, R>
  <A, R, E>(self: SynchronizedRef<A>, f: (a: A) => Effect.Effect<A, E, R>): Effect.Effect<A, E, R>
} = dual(
  2,
  <A, R, E>(self: SynchronizedRef<A>, f: (a: A) => Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
    self.semaphore.withPermit(Effect.suspend(() => {
      const value = getUnsafe(self)
      return Effect.map(f(value), (newValue) => {
        self.backing.ref.current = newValue
        return value
      })
    }))
)