gate.ts
This file
Path
packages/gate/src/gate.ts
56 lines · 1.5 KiB · TypeScript
Last changed by
Merge observability into mainoscabrielbefore adoption1985111
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657/** * Stops after the first stage that refuses, so a push never pays for a stage * past its cheapest problem. */import type { StageName } from "@repo/protocol/domain/outcome";import type { Refusal } from "@repo/protocol/refusal";import { Array, Effect, Option } from "effect";
import type { PushFacts } from "./facts.ts";import type { Stage } from "./stage.ts";import { parse } from "./stages/parse.ts";import { scan } from "./stages/shape.ts";
/** What the first stage that refused a push refused it for. */export interface StageRefusal { readonly refusals: Array.NonEmptyReadonlyArray<Refusal>; readonly stage: StageName;}
export const refusingStage = <R>( stages: readonly Stage<R>[], facts: PushFacts): Effect.Effect<Option.Option<StageRefusal>, never, R> => Effect.gen(function* () { for (const stage of stages) { const refusals = yield* stage .run(facts) .pipe(Effect.withSpan(`Gate.${stage.name}`));
if (Array.isReadonlyArrayNonEmpty(refusals)) { return Option.some({ refusals: stage.accumulate ? refusals : Array.of(Array.headNonEmpty(refusals)), stage: stage.name, }); } }
return Option.none(); });
export const runGate = <R>( stages: readonly Stage<R>[], facts: PushFacts): Effect.Effect< Option.Option<Array.NonEmptyReadonlyArray<Refusal>>, never, R> => Effect.map( refusingStage(stages, facts), Option.map(({ refusals }) => refusals) );
export const pushStages: readonly Stage<never>[] = [parse, scan];