Open History to see the last commit
1import { WS_METHODS } from "@t3tools/contracts";
2import { Atom } from "effect/reactivity";
3
4import {
5 createAtomCommandScheduler,
6 createEnvironmentRpcCommand,
7 createEnvironmentRpcQueryAtomFamily,
8 createEnvironmentRpcSubscriptionAtomFamily,
9} from "./runtime.ts";
10import type { EnvironmentRegistry } from "../connection/registry.ts";
11import * as Persistence from "../platform/persistence.ts";
12import { vcsCommandConcurrency, vcsCommandScheduler } from "./vcsCommandScheduler.ts";
13import { invalidateCachedVcsRefs } from "./vcsRefInvalidation.ts";
14
15export function createSourceControlEnvironmentAtoms<R, E>(
16 runtime: Atom.AtomRuntime<EnvironmentRegistry | Persistence.EnvironmentCacheStore | R, E>,
17) {
18 const commandScheduler = createAtomCommandScheduler();
19 return {
20 discovery: createEnvironmentRpcQueryAtomFamily(runtime, {
21 label: "environment-data:server:source-control-discovery",
22 tag: WS_METHODS.serverDiscoverSourceControl,
23 }),
24 appAccess: createEnvironmentRpcQueryAtomFamily(runtime, {
25 label: "environment-data:source-control:app-access",
26 tag: WS_METHODS.sourceControlAppAccess,
27 }),
28 // One check, as after cloning, where no view keeps the query mounted.
29 checkAppAccess: createEnvironmentRpcCommand(runtime, {
30 label: "environment-data:source-control:check-app-access",
31 tag: WS_METHODS.sourceControlAppAccess,
32 }),
33 repository: createEnvironmentRpcQueryAtomFamily(runtime, {
34 label: "environment-data:source-control:repository",
35 tag: WS_METHODS.sourceControlLookupRepository,
36 }),
37 // Clone-backed project creation. The RPC returns once the project exists
38 // and the clone runs in the background; `projectClones` carries progress.
39 startProjectClone: createEnvironmentRpcCommand(runtime, {
40 label: "environment-data:source-control:project-clone-start",
41 tag: WS_METHODS.projectCloneStart,
42 scheduler: commandScheduler,
43 concurrency: {
44 mode: "serial",
45 key: ({ environmentId }) => environmentId,
46 },
47 }),
48 // Cancel and retry share the start queue so a double click cannot race
49 // two actions against the same clone.
50 cancelProjectClone: createEnvironmentRpcCommand(runtime, {
51 label: "environment-data:source-control:project-clone-cancel",
52 tag: WS_METHODS.projectCloneCancel,
53 scheduler: commandScheduler,
54 concurrency: {
55 mode: "serial",
56 key: ({ environmentId }) => environmentId,
57 },
58 }),
59 retryProjectClone: createEnvironmentRpcCommand(runtime, {
60 label: "environment-data:source-control:project-clone-retry",
61 tag: WS_METHODS.projectCloneRetry,
62 scheduler: commandScheduler,
63 concurrency: {
64 mode: "serial",
65 key: ({ environmentId }) => environmentId,
66 },
67 }),
68 // Unscheduled: a pending sign-in waits on the user for minutes and must not hold up clones.
69 startSignIn: createEnvironmentRpcCommand(runtime, {
70 label: "environment-data:source-control:sign-in-start",
71 tag: WS_METHODS.sourceControlSignInStart,
72 }),
73 waitForSignIn: createEnvironmentRpcCommand(runtime, {
74 label: "environment-data:source-control:sign-in-wait",
75 tag: WS_METHODS.sourceControlSignInWait,
76 }),
77 cancelSignIn: createEnvironmentRpcCommand(runtime, {
78 label: "environment-data:source-control:sign-in-cancel",
79 tag: WS_METHODS.sourceControlSignInCancel,
80 }),
81 projectClones: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
82 label: "environment-data:source-control:project-clones",
83 tag: WS_METHODS.subscribeProjectClones,
84 }),
85 publishRepository: createEnvironmentRpcCommand(runtime, {
86 label: "environment-data:source-control:publish-repository",
87 tag: WS_METHODS.sourceControlPublishRepository,
88 scheduler: vcsCommandScheduler,
89 concurrency: vcsCommandConcurrency,
90 onSettled: (target, registry) =>
91 invalidateCachedVcsRefs(registry, {
92 environmentId: target.environmentId,
93 cwd: target.input.cwd,
94 }),
95 }),
96 };
97}