Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

sourceControl.ts

101 lines4.1 KB

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 checkStarred: createEnvironmentRpcCommand(runtime, {
34 label: "environment-data:source-control:check-starred",
35 tag: WS_METHODS.sourceControlStarred,
36 }),
37 repository: createEnvironmentRpcQueryAtomFamily(runtime, {
38 label: "environment-data:source-control:repository",
39 tag: WS_METHODS.sourceControlLookupRepository,
40 }),
41 // Clone-backed project creation. The RPC returns once the project exists
42 // and the clone runs in the background; `projectClones` carries progress.
43 startProjectClone: createEnvironmentRpcCommand(runtime, {
44 label: "environment-data:source-control:project-clone-start",
45 tag: WS_METHODS.projectCloneStart,
46 scheduler: commandScheduler,
47 concurrency: {
48 mode: "serial",
49 key: ({ environmentId }) => environmentId,
50 },
51 }),
52 // Cancel and retry share the start queue so a double click cannot race
53 // two actions against the same clone.
54 cancelProjectClone: createEnvironmentRpcCommand(runtime, {
55 label: "environment-data:source-control:project-clone-cancel",
56 tag: WS_METHODS.projectCloneCancel,
57 scheduler: commandScheduler,
58 concurrency: {
59 mode: "serial",
60 key: ({ environmentId }) => environmentId,
61 },
62 }),
63 retryProjectClone: createEnvironmentRpcCommand(runtime, {
64 label: "environment-data:source-control:project-clone-retry",
65 tag: WS_METHODS.projectCloneRetry,
66 scheduler: commandScheduler,
67 concurrency: {
68 mode: "serial",
69 key: ({ environmentId }) => environmentId,
70 },
71 }),
72 // Unscheduled: a pending sign-in waits on the user for minutes and must not hold up clones.
73 startSignIn: createEnvironmentRpcCommand(runtime, {
74 label: "environment-data:source-control:sign-in-start",
75 tag: WS_METHODS.sourceControlSignInStart,
76 }),
77 waitForSignIn: createEnvironmentRpcCommand(runtime, {
78 label: "environment-data:source-control:sign-in-wait",
79 tag: WS_METHODS.sourceControlSignInWait,
80 }),
81 cancelSignIn: createEnvironmentRpcCommand(runtime, {
82 label: "environment-data:source-control:sign-in-cancel",
83 tag: WS_METHODS.sourceControlSignInCancel,
84 }),
85 projectClones: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
86 label: "environment-data:source-control:project-clones",
87 tag: WS_METHODS.subscribeProjectClones,
88 }),
89 publishRepository: createEnvironmentRpcCommand(runtime, {
90 label: "environment-data:source-control:publish-repository",
91 tag: WS_METHODS.sourceControlPublishRepository,
92 scheduler: vcsCommandScheduler,
93 concurrency: vcsCommandConcurrency,
94 onSettled: (target, registry) =>
95 invalidateCachedVcsRefs(registry, {
96 environmentId: target.environmentId,
97 cwd: target.input.cwd,
98 }),
99 }),
100 };