Repository navigation
Expand file tree
/
Copy pathfilesViewedStore.ts
More file actions
256 lines (237 loc) · 9.29 KB
/
Copy pathfilesViewedStore.ts
File metadata and controls
256 lines (237 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import {
type AtomCommandResult,
isAtomCommandInterrupted,
runAtomCommand,
} from "@t3tools/client-runtime/state/runtime";
import type {
EnvironmentId,
PullRequestFilesViewedResult,
PullRequestRef,
} from "@t3tools/contracts";
import { AsyncResult } from "effect/reactivity";
import { appAtomRegistry } from "~/rpc/atomRegistry";
import { pullRequestEnvironment } from "~/state/pullRequests";
import { toastManager } from "../ui/toast";
import {
revertFileViewedOverlay,
settleFileViewedOverlay,
toFileViewedBatch,
toFileViewedStates,
type FileViewedOverlay,
} from "./pullRequestFilesViewed.logic";
export interface FilesViewedTarget {
readonly environmentId: EnvironmentId;
readonly reference: PullRequestRef;
}
type FilesViewedBatch = ReadonlyArray<{ readonly path: string; readonly viewed: boolean }>;
export interface FilesViewedStoreDeps {
readonly flushDelayMs: number;
/** Calls `onAnswer` with the host's current answer at once, then with every later one. */
readonly subscribeAnswers: (
target: FilesViewedTarget,
onAnswer: (answer: PullRequestFilesViewedResult | null) => void,
) => () => void;
readonly send: (
target: FilesViewedTarget,
batch: FilesViewedBatch,
) => Promise<AtomCommandResult<unknown, unknown>>;
readonly refresh: (target: FilesViewedTarget) => void;
readonly reportFailure: () => void;
}
export interface FilesViewedScopeHandle {
/** Holds the scope open while subscribed; the last reader to leave sends its last presses. */
readonly subscribe: (listener: () => void) => () => void;
readonly getSnapshot: () => FileViewedOverlay;
readonly setViewed: (path: string, viewed: boolean) => void;
}
const NO_OVERLAY: FileViewedOverlay = new Map();
interface FilesViewedScope {
readonly target: FilesViewedTarget;
readonly listeners: Set<() => void>;
readonly unsubscribeAnswers: () => void;
overlay: FileViewedOverlay;
queued: Map<string, boolean>;
readonly sentBy: Map<string, number>;
requests: number;
timer: ReturnType<typeof setTimeout> | null;
answer: PullRequestFilesViewedResult | null;
/**
* The host's answer as it stood when a completed write was acknowledged, per path. The first
* answer that differs from it is the first read that could have seen the write, which is what
* retires the press rather than the host happening to agree with it.
*/
readonly answeredFrom: Map<string, PullRequestFilesViewedResult | null>;
}
/**
* Presses on one change request on one environment. The environment is part of the scope: two of
* them can hand out the same project id, and a press made against one must never be answered for
* by the other.
*/
export function createFilesViewedStore(deps: FilesViewedStoreDeps) {
const scopes = new Map<string, FilesViewedScope>();
const handles = new Map<string, FilesViewedScopeHandle>();
const publish = (scope: FilesViewedScope, overlay: FileViewedOverlay) => {
if (overlay === scope.overlay) return;
scope.overlay = overlay;
for (const listener of scope.listeners) listener();
};
const isLive = (key: string, scope: FilesViewedScope) => scopes.get(key) === scope;
const settle = (scope: FilesViewedScope, answer: PullRequestFilesViewedResult | null) => {
if (answer === scope.answer) return;
scope.answer = answer;
const pending = new Set([...scope.queued.keys(), ...scope.sentBy.keys()]);
const answered = new Set<string>();
for (const [path, from] of scope.answeredFrom) {
if (pending.has(path) || from === answer) continue;
answered.add(path);
scope.answeredFrom.delete(path);
}
publish(
scope,
settleFileViewedOverlay(scope.overlay, toFileViewedStates(answer), pending, answered),
);
};
const flush = (key: string, scope: FilesViewedScope) => {
scope.timer = null;
const batch = toFileViewedBatch(scope.queued);
if (batch.length === 0) return;
scope.queued = new Map();
const request = ++scope.requests;
for (const file of batch) scope.sentBy.set(file.path, request);
void deps.send(scope.target, batch).then((result) => {
const mine = batch
.map((file) => file.path)
.filter((path) => scope.sentBy.get(path) === request);
for (const path of mine) scope.sentBy.delete(path);
if (!isLive(key, scope)) {
// The overlay this press stood in is gone, but the cached answer still predates the
// write, and a reader coming back to this change request would be shown that.
if (result._tag === "Success" || !isAtomCommandInterrupted(result)) {
deps.refresh(scope.target);
}
return;
}
if (result._tag === "Failure") {
const owned = new Set(mine.filter((path) => !scope.queued.has(path)));
publish(scope, revertFileViewedOverlay(scope.overlay, batch, owned));
if (owned.size > 0 && !isAtomCommandInterrupted(result)) {
deps.reportFailure();
deps.refresh(scope.target);
}
return;
}
// Answered for from the next read on, whatever it says. A push landing between the write
// and that read comes back as `dismissed`, and the press must not stand over it.
for (const path of mine) scope.answeredFrom.set(path, scope.answer);
deps.refresh(scope.target);
});
};
const acquire = (
key: string,
target: FilesViewedTarget,
handle: FilesViewedScopeHandle,
listener: () => void,
) => {
// Registered from `subscribe`, which runs at commit, so a render that never commits leaves
// nothing behind. Re-registering here also carries a handle across StrictMode's
// subscribe-release-subscribe, so later renders keep returning it.
if (!handles.has(key)) handles.set(key, handle);
let scope = scopes.get(key);
if (scope === undefined) {
const listeners = new Set<() => void>();
let created: FilesViewedScope | undefined;
let initial: PullRequestFilesViewedResult | null = null;
const unsubscribeAnswers = deps.subscribeAnswers(target, (answer) => {
if (created === undefined) initial = answer;
else settle(created, answer);
});
created = {
target,
listeners,
unsubscribeAnswers,
overlay: NO_OVERLAY,
queued: new Map(),
sentBy: new Map(),
requests: 0,
timer: null,
answer: initial,
answeredFrom: new Map(),
};
scope = created;
scopes.set(key, scope);
}
const owned = scope;
owned.listeners.add(listener);
return () => {
if (!owned.listeners.delete(listener) || owned.listeners.size > 0) return;
// Leaving a change request, the environment it lives on, or the page itself records what
// was pressed and then drops the rest, sending those last presses to the scope being left.
if (owned.timer !== null) {
clearTimeout(owned.timer);
flush(key, owned);
}
scopes.delete(key);
handles.delete(key);
owned.unsubscribeAnswers();
};
};
return {
/**
* One stable handle per open scope, so readers keep their subscription across renders. Pure
* during render: an uncommitted handle is not cached, and any handle for a key behaves the same.
*/
scope(target: FilesViewedTarget): FilesViewedScopeHandle {
const key = filesViewedScopeKey(target);
const cached = handles.get(key);
if (cached !== undefined) return cached;
const owned = { environmentId: target.environmentId, reference: { ...target.reference } };
const handle: FilesViewedScopeHandle = {
subscribe: (listener) => acquire(key, owned, handle, listener),
getSnapshot: () => scopes.get(key)?.overlay ?? NO_OVERLAY,
setViewed: (path, viewed) => {
const scope = scopes.get(key);
if (scope === undefined) return;
scope.queued.set(path, viewed);
publish(scope, new Map(scope.overlay).set(path, viewed));
if (scope.timer !== null) clearTimeout(scope.timer);
scope.timer = setTimeout(() => flush(key, scope), deps.flushDelayMs);
},
};
return handle;
},
};
}
function filesViewedScopeKey({ environmentId, reference }: FilesViewedTarget): string {
return JSON.stringify([
environmentId,
reference.projectId,
reference.host ?? null,
reference.expectedAccountId ?? null,
reference.allowStale ?? null,
reference.repository,
reference.number,
]);
}
const filesViewedAtom = ({ environmentId, reference }: FilesViewedTarget) =>
pullRequestEnvironment.filesViewed({ environmentId, input: reference });
const filesViewedStore = createFilesViewedStore({
flushDelayMs: 400,
subscribeAnswers: (target, onAnswer) =>
appAtomRegistry.subscribe(
filesViewedAtom(target),
(result) => onAnswer(AsyncResult.getOrElse(result, () => null)),
{ immediate: true },
),
send: ({ environmentId, reference }, files) =>
runAtomCommand(
appAtomRegistry,
pullRequestEnvironment.setFilesViewed,
{ environmentId, input: { ...reference, files } },
{ reportFailure: false },
),
refresh: (target) => appAtomRegistry.refresh(filesViewedAtom(target)),
reportFailure: () => toastManager.add({ type: "error", title: "Could not update viewed files" }),
});
export function filesViewedScope(target: FilesViewedTarget): FilesViewedScopeHandle {
return filesViewedStore.scope(target);
}