Skip to content
Myr

inline.ts

This file

Path

packages/drafting/src/draft-runs/inline.ts

86 lines · 2.6 KiB · TypeScript

Last changed by

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

History of this file

Take it

Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f

line 1why, one step back
1/**

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

lines 2–4why, one step back
2 * `Projects` is built over the project services that start runs, so the runs
3 * take their ports when a round starts, and `withDraftRunsInline` ties the4 * knot, handing its runs the `Projects` it builds.

Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f

lines 5–86why, one step back
5 */
6import type { RepoHost } from "@repo/artifacts/repo-host";7import { DraftRuns } from "@repo/project/draft-runs";8import type { DraftParams } from "@repo/project/draft-runs";9import { Projects } from "@repo/project/projects";10import { Context, Deferred, Effect, Layer, Ref } from "effect";1112import type { Drafter } from "../drafter.ts";13import { runDraft } from "../run.ts";14import type { DraftSteps } from "../run.ts";1516export type InlinePorts = Projects | Drafter | RepoHost;1718const inlineSteps: DraftSteps = {19 step: (_name, _schema, effect, retry) =>20 Effect.retry(effect, { times: retry?.limit ?? 0 }),21};2223const keyOf = ({ project, proposal, round }: DraftParams) =>24 `${project}/${proposal}/${round}`;2526export const draftRunsInline = (27 portsOf: Effect.Effect<Context.Context<InlinePorts>>28): Layer.Layer<never> =>29 Layer.effect(30 DraftRuns,31 Effect.gen(function* () {32 const running = yield* Ref.make<ReadonlySet<string>>(new Set());3334 const claimRun = (key: string) =>35 Ref.modify(running, (under) =>36 under.has(key) ? [false, under] : [true, new Set(under).add(key)]37 );3839 const endRun = (key: string) =>40 Ref.update(running, (under) => {41 const rest = new Set(under);4243 rest.delete(key);4445 return rest;46 });4748 return {49 start: (params) =>50 Effect.gen(function* () {51 const key = keyOf(params);5253 if (!(yield* claimRun(key))) {54 return;55 }5657 yield* runDraft(params, inlineSteps).pipe(58 Effect.provideContext(yield* portsOf),59 Effect.catchCause((cause) =>60 Effect.logWarning("a draft's run died", cause)61 ),62 Effect.ensuring(endRun(key))63 );64 }),65 };66 })67 );6869export const withDraftRunsInline = <E, R>(70 projects: Layer.Layer<Projects, E, R>71): Layer.Layer<Projects, E, R | Drafter | RepoHost> =>72 Layer.effectContext(73 Effect.gen(function* () {74 const ready = yield* Deferred.make<Context.Context<InlinePorts>>();7576 const built = yield* Layer.build(77 projects.pipe(Layer.provide(draftRunsInline(Deferred.await(ready))))78 );7980 const around = yield* Effect.context<Drafter | RepoHost>();8182 yield* Deferred.succeed(ready, Context.merge(around, built));8384 return Context.make(Projects, Context.get(built, Projects));85 })86 );