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

pullRequestFilesViewed.logic.ts

85 lines2.8 KB

1import type { PullRequestFileViewedState, PullRequestFilesViewedResult } from "@t3tools/contracts";
2​
3export type FileViewedStates = ReadonlyMap<string, PullRequestFileViewedState>;
4​
5export type FileViewedOverlay = ReadonlyMap<string, boolean>;
6​
7export function toFileViewedStates(
8 result: PullRequestFilesViewedResult | null,
9): FileViewedStates | null {
10 if (result === null) return null;
11 return new Map(result.files.map((file) => [file.path, file.state]));
13​
14function isViewedState(state: PullRequestFileViewedState | undefined): boolean {
15 return state === "viewed";
17​
18export function isStaleViewedState(state: PullRequestFileViewedState | undefined): boolean {
19 return state === "dismissed";
21​
22export function isFileViewed(
23 path: string,
24 states: FileViewedStates | null,
25 overlay: FileViewedOverlay,
26): boolean {
27 const pressed = overlay.get(path);
28 return pressed ?? isViewedState(states?.get(path));
30​
31export function countViewedFiles(
32 paths: ReadonlyArray<string>,
33 states: FileViewedStates | null,
34 overlay: FileViewedOverlay,
35): number {
36 return paths.reduce(
37 (total, path) => (isFileViewed(path, states, overlay) ? total + 1 : total),
38 0,
39 );
41​
42/**
43 * A press is held until a read that could have seen it comes back, rather than cleared when the
44 * write succeeds: that read is a separate round trip, and dropping the press in between flashes
45 * the checkbox back.
46 */
47export function settleFileViewedOverlay(
48 overlay: FileViewedOverlay,
49 states: FileViewedStates | null,
50 pending: ReadonlySet<string>,
51 answered: ReadonlySet<string>,
52): FileViewedOverlay {
53 if (states === null || overlay.size === 0) return overlay;
54 const next = new Map(overlay);
55 for (const [path, pressed] of overlay) {
56 if (pending.has(path)) continue;
57 if (answered.has(path) || isViewedState(states.get(path)) === pressed) next.delete(path);
58 }
59 return next.size === overlay.size ? overlay : next;
61​
62/**
63 * `owned` are the paths that request still answers for, which keeps a failure from reaching past
64 * its own presses: a path pressed again since belongs to a later request or to the next flush,
65 * and putting that box back to the host's answer would take a press out from under the reader's
66 * hand.
67 */
68export function revertFileViewedOverlay(
69 overlay: FileViewedOverlay,
70 batch: ReadonlyArray<{ readonly path: string; readonly viewed: boolean }>,
71 owned: ReadonlySet<string>,
72): FileViewedOverlay {
73 const next = new Map(overlay);
74 for (const { path, viewed } of batch) {
75 if (!owned.has(path)) continue;
76 if (next.get(path) === viewed) next.delete(path);
77 }
78 return next.size === overlay.size ? overlay : next;
80​
81export function toFileViewedBatch(
82 overlay: FileViewedOverlay,
83): ReadonlyArray<{ readonly path: string; readonly viewed: boolean }> {
84 return [...overlay].map(([path, viewed]) => ({ path, viewed }));