Hyperlinkv0.8.0-beta.28

ScopedRef

ScopedRef.makeconsteffect/ScopedRef.ts:145
<A>(evaluate: LazyArg<A>): Effect.Effect<ScopedRef<A>, never, Scope.Scope>

Creates a new ScopedRef from the specified value.

When to use

Use to create a ScopedRef when the initial value is already available or can be produced without acquiring resources.

Details

The evaluate function runs when the returned effect runs. The returned effect requires a Scope, and the reference closes the currently stored value's scope when that outer scope closes.

Gotchas

Do not use make for an initial value whose creation acquires resources; use fromAcquire so acquisition and finalization are tracked.

constructorsfromAcquireset
export const make = <A>(evaluate: LazyArg<A>): Effect.Effect<ScopedRef<A>, never, Scope.Scope> =>
  Effect.suspend(() => {
    const scope = Scope.makeUnsafe()
    const value = evaluate()
    const self = makeUnsafe(scope, value)
    return Effect.as(Effect.addFinalizer((exit) => Scope.close(self.backing.backing.ref.current[0], exit)), self)
  })