Skip to content

Commit 5f60c38

Browse files
committed
fix(web): settle viewed presses after completed reads
1 parent 14c7b19 commit 5f60c38

2 files changed

Lines changed: 15 additions & 28 deletions

File tree

‎apps/web/src/components/pullRequest/filesViewedStore.test.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ function setup(initial: PullRequestFilesViewedResult | null = answer(false)) {
7171

7272
describe("createFilesViewedStore", () => {
7373
it("batches presses and holds them until a read that could have seen them", async () => {
74-
using t = setup();
74+
const unchanged = { files: [{ path: "a.ts", state: "dismissed" as const }], truncated: false };
75+
using t = setup(unchanged);
7576
const scope = t.store.scope(first);
7677
const release = scope.subscribe(() => {});
7778
scope.setViewed("a.ts", true, "shown-revision");
@@ -88,10 +89,7 @@ describe("createFilesViewedStore", () => {
8889
expect(scope.getSnapshot().get("a.ts")).toBe(true);
8990

9091
// A push landed between the write and the next read: the host's answer wins.
91-
t.answerFor(first, {
92-
files: [{ path: "a.ts", state: "dismissed" }],
93-
truncated: false,
94-
});
92+
t.answerFor(first, unchanged);
9593
expect(scope.getSnapshot().size).toBe(0);
9694
release();
9795
});
@@ -205,9 +203,6 @@ describe("createFilesViewedStore", () => {
205203
await vi.advanceTimersByTimeAsync(400);
206204
t.settles[0]?.(AsyncResult.success(undefined));
207205
await vi.advanceTimersByTimeAsync(0);
208-
// The same answer published again is not a new read.
209-
t.answerFor(first, agreeing);
210-
expect(scope.getSnapshot().get("a.ts")).toBe(true);
211206
t.answerFor(first, answer(false));
212207
expect(scope.getSnapshot().size).toBe(0);
213208
release();

‎apps/web/src/components/pullRequest/filesViewedStore.ts‎

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type {
88
PullRequestFilesViewedResult,
99
PullRequestRef,
1010
} from "@t3tools/contracts";
11-
import * as Option from "effect/Option";
1211
import { AsyncResult } from "effect/reactivity";
1312

1413
import { appAtomRegistry } from "~/rpc/atomRegistry";
@@ -37,7 +36,7 @@ type FilesViewedBatch = ReadonlyArray<{
3736

3837
export interface FilesViewedStoreDeps {
3938
readonly flushDelayMs: number;
40-
/** Calls `onAnswer` with the host's current answer at once, then with every later one. */
39+
/** Calls `onAnswer` with the current answer, then after each completed read. */
4140
readonly subscribeAnswers: (
4241
target: FilesViewedTarget,
4342
onAnswer: (answer: PullRequestFilesViewedResult | null) => void,
@@ -68,13 +67,8 @@ interface FilesViewedScope {
6867
readonly sentBy: Map<string, number>;
6968
requests: number;
7069
timer: ReturnType<typeof setTimeout> | null;
71-
answer: PullRequestFilesViewedResult | null;
72-
/**
73-
* The host's answer as it stood when a completed write was acknowledged, per path. The first
74-
* answer that differs from it is the first read that could have seen the write, which is what
75-
* retires the press rather than the host happening to agree with it.
76-
*/
77-
readonly answeredFrom: Map<string, PullRequestFilesViewedResult | null>;
70+
// Completed reads retire acknowledged presses even when equal data is structurally shared.
71+
readonly answeredFrom: Set<string>;
7872
}
7973

8074
/**
@@ -95,12 +89,11 @@ export function createFilesViewedStore(deps: FilesViewedStoreDeps) {
9589
const isLive = (key: string, scope: FilesViewedScope) => scopes.get(key) === scope;
9690

9791
const settle = (scope: FilesViewedScope, answer: PullRequestFilesViewedResult | null) => {
98-
if (answer === scope.answer) return;
99-
scope.answer = answer;
92+
if (answer === null) return;
10093
const pending = new Set([...scope.queued.keys(), ...scope.sentBy.keys()]);
10194
const answered = new Set<string>();
102-
for (const [path, from] of scope.answeredFrom) {
103-
if (pending.has(path) || from === answer) continue;
95+
for (const path of scope.answeredFrom) {
96+
if (pending.has(path)) continue;
10497
answered.add(path);
10598
scope.answeredFrom.delete(path);
10699
}
@@ -141,7 +134,7 @@ export function createFilesViewedStore(deps: FilesViewedStoreDeps) {
141134
}
142135
// Answered for from the next read on, whatever it says. A push landing between the write
143136
// and that read comes back as `dismissed`, and the press must not stand over it.
144-
for (const path of mine) scope.answeredFrom.set(path, scope.answer);
137+
for (const path of mine) scope.answeredFrom.add(path);
145138
deps.refresh(scope.target);
146139
});
147140
};
@@ -160,10 +153,8 @@ export function createFilesViewedStore(deps: FilesViewedStoreDeps) {
160153
if (scope === undefined) {
161154
const listeners = new Set<() => void>();
162155
let created: FilesViewedScope | undefined;
163-
let initial: PullRequestFilesViewedResult | null = null;
164156
const unsubscribeAnswers = deps.subscribeAnswers(target, (answer) => {
165-
if (created === undefined) initial = answer;
166-
else settle(created, answer);
157+
if (created !== undefined) settle(created, answer);
167158
});
168159
created = {
169160
target,
@@ -174,8 +165,7 @@ export function createFilesViewedStore(deps: FilesViewedStoreDeps) {
174165
sentBy: new Map(),
175166
requests: 0,
176167
timer: null,
177-
answer: initial,
178-
answeredFrom: new Map(),
168+
answeredFrom: new Set(),
179169
};
180170
scope = created;
181171
scopes.set(key, scope);
@@ -243,7 +233,9 @@ const filesViewedStore = createFilesViewedStore({
243233
subscribeAnswers: (target, onAnswer) =>
244234
appAtomRegistry.subscribe(
245235
filesViewedAtom(target),
246-
(result) => onAnswer(Option.getOrNull(AsyncResult.value(result))),
236+
(result) => {
237+
if (AsyncResult.isSuccess(result) && !result.waiting) onAnswer(result.value);
238+
},
247239
{ immediate: true },
248240
),
249241
send: ({ environmentId, reference }, files) =>

0 commit comments

Comments
 (0)