Hyperlinkv0.8.0-beta.28

SynchronizedRef

SynchronizedRef.modifyEffectconsteffect/SynchronizedRef.ts:306
<A, B, E, R>(f: (a: A) => Effect.Effect<readonly [B, A], E, R>): (
  self: SynchronizedRef<A>
) => Effect.Effect<B, E, R>
<A, B, E, R>(
  self: SynchronizedRef<A>,
  f: (a: A) => Effect.Effect<readonly [B, A], E, R>
): Effect.Effect<B, E, R>

Runs an effectful modification atomically while holding the ref's semaphore, stores the new value if the effect succeeds, and returns the computed result.

When to use

Use to effectfully compute both a separate return value and the next stored SynchronizedRef value in one serialized update.

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