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

ProjectMutation.ts

76 lines3.2 KB

1import { type ProjectMutation } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3​
4import { type ProjectCloneTracker } from "./ProjectCloneTracker.ts";
5import { type ProjectService } from "./ProjectService.ts";
6​
7type ProjectMutations = Pick<ProjectService["Service"], "create" | "delete" | "update">;
8​
9export const projectMutationOperation = Effect.fn("projectMutationOperation")(function* (
10 projects: ProjectMutations,
11 mutation: ProjectMutation,
12) {
13 switch (mutation.type) {
14 case "project.create":
15 return yield* projects.create({
16 commandId: mutation.commandId,
17 projectId: mutation.projectId,
18 title: mutation.title,
19 workspaceRoot: mutation.workspaceRoot,
20 ...(mutation.createWorkspaceRootIfMissing === undefined
21 ? {}
22 : { createWorkspaceRootIfMissing: mutation.createWorkspaceRootIfMissing }),
23 ...(mutation.defaultModelSelection === undefined
24 ? {}
25 : { defaultModelSelection: mutation.defaultModelSelection }),
26 ...(mutation.scripts === undefined ? {} : { scripts: mutation.scripts }),
27 ...(mutation.trackedFolders === undefined
28 ? {}
29 : { trackedFolders: mutation.trackedFolders }),
30 });
31​
32 case "project.update":
33 return yield* projects.update({
34 commandId: mutation.commandId,
35 projectId: mutation.projectId,
36 ...(mutation.title === undefined ? {} : { title: mutation.title }),
37 ...(mutation.workspaceRoot === undefined ? {} : { workspaceRoot: mutation.workspaceRoot }),
38 ...(mutation.defaultModelSelection === undefined
39 ? {}
40 : { defaultModelSelection: mutation.defaultModelSelection }),
41 ...(mutation.autoPull === undefined ? {} : { autoPull: mutation.autoPull }),
42 ...(mutation.projectIcon === undefined ? {} : { projectIcon: mutation.projectIcon }),
43 ...(mutation.faviconPath === undefined ? {} : { faviconPath: mutation.faviconPath }),
44 ...(mutation.defaultThreadEnvMode === undefined
45 ? {}
46 : { defaultThreadEnvMode: mutation.defaultThreadEnvMode }),
47 ...(mutation.scripts === undefined ? {} : { scripts: mutation.scripts }),
48 ...(mutation.trackedFolders === undefined
49 ? {}
50 : { trackedFolders: mutation.trackedFolders }),
51 ...(mutation.pinned === undefined ? {} : { pinned: mutation.pinned }),
52 ...(mutation.archived === undefined ? {} : { archived: mutation.archived }),
53 });
54​
55 case "project.delete":
56 return yield* projects.delete({
57 commandId: mutation.commandId,
58 projectId: mutation.projectId,
59 ...(mutation.force === undefined ? {} : { force: mutation.force }),
60 });
61 }
62});
63​
64// A disconnect after deletion commits must not leave the in-memory clone record behind.
65export const mutateProjectWithClone = Effect.fn("mutateProjectWithClone")(function* (
66 projects: ProjectMutations,
67 clones: Pick<ProjectCloneTracker["Service"], "discard">,
68 mutation: ProjectMutation,
69) {
70 if (mutation.type !== "project.delete")
71 return yield* projectMutationOperation(projects, mutation);
72 return yield* projectMutationOperation(projects, mutation).pipe(
73 Effect.tap(() => clones.discard(mutation.projectId)),
74 Effect.uninterruptible,
75 );
76});