variable / @typed/guard

provide

Provides a Context or Layer to a Guard’s Effect.

Package version
1.0.0-beta.13
Category
Services
Since
1.0.0

Import

import { provide } from "@typed/guard";

Signatures

export declare const provide: {
    <R2>(provided: Context.Context<R2>): <I, O, E, R>(guard: GuardInput<I, O, E, R>) => Guard<I, O, E, Exclude<R, R2>>;
    <R2, E2, R3>(provided: Layer.Layer<R2, E2, R3>): <I, O, E, R>(guard: GuardInput<I, O, E, R>) => Guard<I, O, E | E2, Exclude<R, R2> | R3>;
    <I, O, E, R, R2>(guard: GuardInput<I, O, E, R>, provided: Context.Context<R2>): Guard<I, O, E, Exclude<R, R2>>;
    <I, O, E, R, R2, E2, R3>(guard: GuardInput<I, O, E, R>, provided: Layer.Layer<R2, E2, R3>): Guard<I, O, E | E2, Exclude<R, R2> | R3>;
};

Why

Provision removes supplied services from R; Layers may also contribute acquisition errors and their own requirements exactly as Effect does.

Ownership and lifetime

A Context acquires no resources here. Layer resources are acquired and released according to the Effect Scope that runs the Guard.

Examples

import { provide } from "@typed/guard"
import type { Guard } from "@typed/guard"
import { Context, Effect, Option } from "effect"
const Flag = Context.Service<{ readonly enabled: boolean }>("Flag")
const requiresFlag: Guard<boolean, boolean, never, Context.Service.Identifier<typeof Flag>> = (input) => Effect.map(Effect.service(Flag), ({ enabled }) => enabled && input ? Option.some(input) : Option.none())
const guard = provide(requiresFlag, Context.make(Flag, { enabled: true }))

See Effect services and Layers.

Source