Hyperlinkv0.8.0-beta.28

SynchronizedRef

SynchronizedRef.updateEffectconsteffect/SynchronizedRef.ts:485
<A, R, E>(f: (a: A) => Effect.Effect<A, E, R>): (
  self: SynchronizedRef<A>
) => Effect.Effect<void, E, R>
<A, R, E>(
  self: SynchronizedRef<A>,
  f: (a: A) => Effect.Effect<A, E, R>
): Effect.Effect<void, E, R>

Runs an effectful update while holding the ref's semaphore and stores the new value if the effect succeeds.

When to use

Use to run an effectful state transition on a SynchronizedRef when storing the new value is the only result you need.

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