~james/core

core/web/src/lib/components/repo/diffQueue.ts -rw-r--r-- 6.3 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
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
import type { FileDiffMetadata } from "@pierre/diffs";
import {
	fetchSides,
	type RepoDiff,
	type RepoDiffDeps,
	type RepoDiffFile
} from "#lib/api/repoDiff.ts";
import { parseDiffInWorker, streamPatchInWorker } from "#lib/components/repo/diffRpc.ts";
import type { DiffEntry } from "#lib/components/repo/fileDiff.ts";

export type { DiffEntry };

export interface DiffQueueHost {
	deps: () => RepoDiffDeps;
	diff: () => RepoDiff;
	entry: (key: string) => DiffEntry | undefined;
	put: (parsed: Record<string, DiffEntry>) => void;
	/** true while a patch is still expected to deliver the whole diff */
	patchExpected?: () => boolean;
}

export interface DiffQueue {
	focus: (key: string) => void;
	want: (key: string) => void;
	/** re-pumps files parked while a patch was expected; call when it settles */
	drain: () => void;
	applyPatch: (url: string, signal: AbortSignal) => Promise<void>;
	/** hands the queue files someone else already parsed, so it stops chasing them */
	adopt: (parsed: Record<string, DiffEntry>) => void;
	whole: (key: string) => boolean;
	destroy: () => void;
}

const CONCURRENCY = 6;

export const createDiffQueue = (host: DiffQueueHost): DiffQueue => {
	let running = 0;
	const pending = new Set<string>();
	const inFlight = new Set<string>();
	const replacing = new Set<string>();
	const tried = new Set<string>();
	// file order is monotonic down the page, so index distance stands in for
	// pixels
	let focusIndex = 0;

	let indexed: {
		files: RepoDiffFile[];
		byKey: Map<string, { file: RepoDiffFile; index: number }>;
	};
	const byKey = () => {
		const files = host.diff().files;
		if (indexed?.files !== files)
			indexed = {
				files,
				byKey: new Map(files.map((file, index) => [file.key, { file, index }]))
			};
		return indexed.byKey;
	};

	const nextKey = (): string | undefined => {
		const index = byKey();
		let best: string | undefined;
		let bestDistance = Infinity;
		for (const key of pending) {
			const distance = Math.abs((index.get(key)?.index ?? 0) - focusIndex);
			if (distance < bestDistance) {
				best = key;
				bestDistance = distance;
			}
		}
		return best;
	};

	const pump = () => {
		// one patch request beats two blob fetches per file, so while one is on its
		// way the wanted files wait for it. what the patch does not carry stays
		// queued and is fetched the moment it settles, either way
		if (host.patchExpected?.()) return;
		while (running < CONCURRENCY) {
			const key = nextKey();
			if (!key) return;
			pending.delete(key);
			const file = byKey().get(key)?.file;
			if (file) void fill(file);
		}
	};

	// the mirror rate-limits bursts, and a fling scroll is exactly a burst. a
	// retried file lands a moment later instead of showing a permanent error
	const RETRIES = 3;
	const backoff = (attempt: number) =>
		new Promise((resolve) => setTimeout(resolve, 300 * 3 ** attempt + Math.random() * 200));

	const fetchWithRetry = async (file: RepoDiffFile) => {
		for (let attempt = 0; ; attempt++) {
			try {
				return await fetchSides(host.deps(), host.diff().contents, file);
			} catch (cause) {
				if (attempt >= RETRIES) throw cause;
				await backoff(attempt);
			}
		}
	};

	const fill = async (file: RepoDiffFile) => {
		running++;
		inFlight.add(file.key);
		try {
			if (host.entry(file.key) && !replacing.has(file.key)) return;
			const contents = await fetchWithRetry(file);
			if (!("oldFile" in contents)) {
				host.put({ [file.key]: { note: contents.note } });
			} else {
				// pierre caches highlights by these keys, and a path alone repeats across a pull's versions
				host.put({
					[file.key]: await parseDiffInWorker(
						{
							...contents.oldFile,
							cacheKey: `${file.key}:old:${file.sides?.lhs.oid ?? ""}`
						},
						{
							...contents.newFile,
							cacheKey: `${file.key}:new:${file.sides?.rhs.oid ?? ""}`
						}
					)
				});
			}
		} catch {
			// the patch may have delivered this file while the fetch was failing, and
			// a real diff beats an error note
			if (!host.entry(file.key)) {
				host.put({ [file.key]: { note: "Contents could not be loaded." } });
			}
		} finally {
			inFlight.delete(file.key);
			replacing.delete(file.key);
			running--;
			pump();
		}
	};

	return {
		destroy: () => {
			pending.clear();
		},
		focus: (key) => {
			focusIndex = byKey().get(key)?.index ?? focusIndex;
		},
		want: (key) => {
			if (host.entry(key) || pending.has(key) || inFlight.has(key)) return;
			if (!byKey().get(key)?.file.sides) return;
			pending.add(key);
			pump();
		},
		drain: () => pump(),
		whole: (key) => {
			if (tried.has(key)) return pending.has(key) || inFlight.has(key);
			if (!byKey().get(key)?.file.sides) return false;
			tried.add(key);
			replacing.add(key);
			pending.add(key);
			pump();
			return true;
		},
		adopt: (parsed) => {
			const landing: Record<string, DiffEntry> = {};
			for (const [key, value] of Object.entries(parsed)) {
				// a file already being fetched keeps its own result: that one carries
				// the whole file, so its context can still be expanded
				if (host.entry(key) || inFlight.has(key) || byKey().get(key)?.file.note) continue;
				landing[key] = value;
				pending.delete(key);
			}
			if (Object.keys(landing).length) host.put(landing);
		},
		applyPatch: async (url, signal) => {
			// the worker streams the body and answers in batches, so the first files
			// land about a second in instead of after the whole patch has arrived
			const land = (files: FileDiffMetadata[]) => {
				if (signal.aborted) return;
				const byName: Record<string, FileDiffMetadata> = {};
				for (const parsed of files) {
					byName[parsed.name] = parsed;
					if (parsed.prevName) byName[parsed.prevName] = parsed;
				}
				const landed: Record<string, DiffEntry> = {};
				for (const file of index.values()) {
					// keep sides already landed or on the wire: that diff carries the whole
					// file, so its context can still be expanded
					if (file.file.note || host.entry(file.file.key) || inFlight.has(file.file.key))
						continue;
					const parsed =
						byName[file.file.name] ??
						(file.file.oldName ? byName[file.file.oldName] : undefined);
					if (!parsed) continue;
					landed[file.file.key] = parsed;
					pending.delete(file.file.key);
				}
				if (Object.keys(landed).length) host.put(landed);
			};

			const index = byKey();
			await streamPatchInWorker(url, land, signal);
		}
	};
};