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

shellReducer.ts

193 lines7.2 KB

1import {
2 OrchestrationV2ThreadShell,
3 RepositoryIdentity,
4 ThreadPullRequestLink,
5 type OrchestrationProjectShell,
6 type OrchestrationV2ShellSnapshot,
7 type OrchestrationV2ShellStreamItem,
8} from "@t3tools/contracts";
9import * as Schema from "effect/Schema";
10​
11const sameThreadShell = Schema.toEquivalence(OrchestrationV2ThreadShell);
12const sameLinks = Schema.toEquivalence(Schema.UndefinedOr(Schema.Array(ThreadPullRequestLink)));
13const sameRepositoryIdentity = Schema.toEquivalence(
14 Schema.UndefinedOr(Schema.NullOr(RepositoryIdentity)),
15);
16​
17export function sameThreadPullRequests(
18 left: OrchestrationV2ThreadShell["pullRequests"],
19 right: OrchestrationV2ThreadShell["pullRequests"],
20): boolean {
21 return left === right || sameLinks(left, right);
23​
24/**
25 * Links are compared apart from the rest of the row because a deferred snapshot arrives without
26 * them; such a row keeps its links until they fill in.
27 */
28export function reuseUnchangedThreadShells(
29 previous: OrchestrationV2ShellSnapshot | null,
30 next: OrchestrationV2ShellSnapshot,
31): OrchestrationV2ShellSnapshot {
32 if (previous === null || previous.threads.length === 0) return next;
33 const previousById = new Map(previous.threads.map((thread) => [thread.id, thread] as const));
34 let changed = false;
35 const threads = next.threads.map((thread) => {
36 const prior = previousById.get(thread.id);
37 if (prior === undefined) return thread;
38 const candidate =
39 thread.pullRequests === undefined && prior.pullRequests !== undefined
40 ? { ...thread, pullRequests: prior.pullRequests }
41 : thread;
42 const retained = sameThreadShell(prior, candidate) ? prior : candidate;
43 if (retained !== thread) changed = true;
44 return retained;
45 });
46 return changed ? { ...next, threads } : next;
48​
49function upsertById<T extends { readonly id: unknown }>(
50 items: ReadonlyArray<T>,
51 item: T,
52): ReadonlyArray<T> {
53 const index = items.findIndex((candidate) => candidate.id === item.id);
54 if (index === -1) return [...items, item];
55 return items.map((candidate, candidateIndex) => (candidateIndex === index ? item : candidate));
57​
58function retainRepositoryIdentity(
59 previous: OrchestrationProjectShell | undefined,
60 next: OrchestrationProjectShell,
61): OrchestrationProjectShell {
62 if (
63 next.repositoryIdentity == null &&
64 previous?.repositoryIdentity != null &&
65 previous.workspaceRoot === next.workspaceRoot
66 ) {
67 return { ...next, repositoryIdentity: previous.repositoryIdentity };
68 }
69 return next;
71​
72export interface MergeShellSnapshotOptions {
73 /**
74 * Metadata-only enrichment refresh: structure and sequence never change;
75 * listed roots accept identity exactly (including null).
76 * Omit this options object for authoritative HTTP/initial WebSocket snapshots.
77 */
78 readonly resolvedRepositoryIdentityRoots: ReadonlyArray<string>;
80​
81export function mergeShellSnapshotProjects(
82 previous: OrchestrationV2ShellSnapshot | null | undefined,
83 next: OrchestrationV2ShellSnapshot,
84 options?: MergeShellSnapshotOptions,
85): OrchestrationV2ShellSnapshot {
86 if (previous === null || previous === undefined) {
87 return next;
88 }
89​
90 const isEnrichment = options !== undefined;
91 const resolvedRootSet = isEnrichment ? new Set(options.resolvedRepositoryIdentityRoots) : null;
92​
93 if (isEnrichment) {
94 const nextById = new Map(next.projects.map((project) => [project.id, project] as const));
95 const projects = previous.projects.map((project) => {
96 const candidate = nextById.get(project.id);
97 if (candidate === undefined || candidate.workspaceRoot !== project.workspaceRoot) {
98 return project;
99 }
100 if (resolvedRootSet?.has(project.workspaceRoot) === true) {
101 // Refreshes re-resolve every root; an unchanged identity keeps the project's identity
102 // so every subscriber (each pull request row, the sidebar) skips re-rendering.
103 if (sameRepositoryIdentity(project.repositoryIdentity, candidate.repositoryIdentity)) {
104 return project;
105 }
106 return { ...project, repositoryIdentity: candidate.repositoryIdentity };
107 }
108 if (project.repositoryIdentity == null && candidate.repositoryIdentity != null) {
109 return { ...project, repositoryIdentity: candidate.repositoryIdentity };
110 }
111 return project;
112 });
113 return projects.every((project, index) => project === previous.projects[index])
114 ? previous
115 : { ...previous, projects };
116 }
117​
118 const previousById = new Map(previous.projects.map((project) => [project.id, project] as const));
119 return {
120 ...next,
121 projects: next.projects.map((project) => {
122 const prior = previousById.get(project.id);
123 if (resolvedRootSet?.has(project.workspaceRoot) === true) {
124 return project;
125 }
126 return retainRepositoryIdentity(prior, project);
127 }),
128 };
130​
131export function applyShellStreamEvent(
132 snapshot: OrchestrationV2ShellSnapshot,
133 event: Exclude<
134 OrchestrationV2ShellStreamItem,
135 { readonly kind: "snapshot" } | { readonly kind: "synchronized" }
136 >,
137): OrchestrationV2ShellSnapshot {
138 if (event.sequence <= snapshot.snapshotSequence) return snapshot;
139​
140 switch (event.kind) {
141 case "project.updated": {
142 // Enrichment is async. A project mutation can land with null
143 // repositoryIdentity while an earlier snapshot already resolved it.
144 // Keep the prior identity for the same workspace root so multi-env
145 // grouping does not split until a full snapshot refresh arrives.
146 const previous = snapshot.projects.find((project) => project.id === event.project.id);
147 const project = retainRepositoryIdentity(previous, event.project);
148 return {
149 ...snapshot,
150 projects: upsertById(snapshot.projects, project),
151 snapshotSequence: event.sequence,
152 };
153 }
154 case "project.removed":
155 return {
156 ...snapshot,
157 projects: snapshot.projects.filter((project) => project.id !== event.projectId),
158 snapshotSequence: event.sequence,
159 };
160 case "thread.updated": {
161 const existing =
162 event.location === "active"
163 ? snapshot.threads.find((thread) => thread.id === event.thread.id)
164 : undefined;
165 if (existing !== undefined && sameThreadShell(existing, event.thread)) {
166 return { ...snapshot, snapshotSequence: event.sequence };
167 }
168 const withoutThread = (threads: OrchestrationV2ShellSnapshot["threads"]) =>
169 threads.filter((thread) => thread.id !== event.thread.id);
170 return {
171 ...snapshot,
172 threads:
173 event.location === "active"
174 ? upsertById(snapshot.threads, event.thread)
175 : withoutThread(snapshot.threads),
176 // The archive has its own bounded query/subscription. Older servers may
177 // still send archive-located deltas here; remove them from the normal
178 // shell instead of growing its persisted cache again.
179 archivedThreads: withoutThread(snapshot.archivedThreads),
180 snapshotSequence: event.sequence,
181 };
182 }
183 case "thread.removed":
184 return {
185 ...snapshot,
186 threads: snapshot.threads.filter((thread) => thread.id !== event.threadId),
187 archivedThreads: snapshot.archivedThreads.filter((thread) => thread.id !== event.threadId),
188 snapshotSequence: event.sequence,
189 };
190 default:
191 return snapshot;
192 }