variable / @typed/guard

provideServiceEffect

Provides a service produced by an Effect to a Guard.

Package version
1.0.0-beta.13
Category
Services
Since
1.0.0

Import

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

Signatures

export declare const provideServiceEffect: {
    <Id, S, E2, R2>(tag: Context.Service<Id, S>, service: Effect.Effect<S, E2, R2>): <I, O, E, R>(guard: GuardInput<I, O, E, R>) => Guard<I, O, E | E2, Exclude<R, Id> | R2>;
    <I, O, E, R, Id, S, E2, R2>(guard: GuardInput<I, O, E, R>, tag: Context.Service<Id, S>, service: Effect.Effect<S, E2, R2>): Guard<I, O, E | E2, Exclude<R, Id> | R2>;
};

Why

Effectful provision makes acquisition errors and required services explicit in the composed Guard channels.

Ownership and lifetime

The service Effect runs for each Guard invocation and is interrupted with that invocation; scoped resources follow the surrounding Scope.

Examples

import { provideServiceEffect } 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 = provideServiceEffect(requiresFlag, Flag, Effect.succeed({ enabled: true }))

Source