Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import type { SourceControlProviderKind } from "@t3tools/contracts";
5
6import * as BuiltInDrivers from "../sourceControl/builtInDrivers.ts";
7import type { PullRequestProviderApi } from "@t3tools/source-control-core/server/PullRequestProvider";
8
9export class PullRequestProviderRegistry extends Context.Service<
10 PullRequestProviderRegistry,
11 {
12 readonly get: (kind: SourceControlProviderKind) => PullRequestProviderApi | null;
13 readonly kinds: ReadonlyArray<SourceControlProviderKind>;
14 }
15>()("t3/pullRequest/PullRequestProviderRegistry") {}
16
17export function fromProviders(
18 providers: ReadonlyArray<PullRequestProviderApi>,
19): PullRequestProviderRegistry["Service"] {
20 const byKind = new Map(providers.map((provider) => [provider.kind, provider]));
21 return {
22 get: (kind) => byKind.get(kind) ?? null,
23 kinds: providers.map((provider) => provider.kind),
24 };
25}
26
27/**
28 * @public Service construction is part of the canonical Effect module API.
29 */
30export const make = Effect.gen(function* () {
31 const drivers = yield* Effect.forEach(BuiltInDrivers.BUILT_IN_SOURCE_CONTROL_DRIVERS, (driver) =>
32 driver.make.pipe(Effect.map((instance) => instance.pullRequests)),
33 );
34 return fromProviders(
35 drivers.filter((provider): provider is PullRequestProviderApi => provider !== null),
36 );
37});
38
39export const layer = Layer.effect(PullRequestProviderRegistry, make).pipe(
40 Layer.provide(BuiltInDrivers.layer),
41);