Skip to content

Commit 98e1d50

Browse files
fix(mobile): a queued share refresh reports its own outcome
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6508108 commit 98e1d50

2 files changed

Lines changed: 46 additions & 27 deletions

File tree

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,57 @@
1-
import { describe, expect, it } from "vite-plus/test";
1+
import { describe, expect, it, vi } from "vite-plus/test";
22

33
import { coalescedRun } from "./coalescedRun";
44

5+
function deferredRuns() {
6+
const runs: Array<PromiseWithResolvers<void>> = [];
7+
const run = () => {
8+
const next = Promise.withResolvers<void>();
9+
runs.push(next);
10+
return next.promise;
11+
};
12+
return { runs, run };
13+
}
14+
515
describe("coalescedRun", () => {
6-
it("runs once more when called while a run is in flight", async () => {
7-
const runs: Array<PromiseWithResolvers<void>> = [];
8-
const refresh = coalescedRun(() => {
9-
const run = Promise.withResolvers<void>();
10-
runs.push(run);
11-
return run.promise;
12-
});
16+
it("queues one more run for calls made while a run is in flight", async () => {
17+
const { runs, run } = deferredRuns();
18+
const refresh = coalescedRun(run);
1319

1420
const first = refresh();
15-
const joined = refresh();
16-
refresh();
17-
expect(joined).toBe(first);
21+
const second = refresh();
22+
const third = refresh();
23+
expect(third).toBe(second);
1824
expect(runs).toHaveLength(1);
1925

2026
runs[0]!.resolve();
2127
await first;
22-
await Promise.resolve();
23-
expect(runs).toHaveLength(2);
28+
await vi.waitFor(() => expect(runs).toHaveLength(2));
2429

2530
runs[1]!.resolve();
26-
await runs[1]!.promise;
27-
await Promise.resolve();
31+
await second;
2832
expect(runs).toHaveLength(2);
2933
});
3034

31-
it("does not run again when nothing was asked during the run", async () => {
35+
it("hands a queued run's failure to the call that asked for it", async () => {
36+
const { runs, run } = deferredRuns();
37+
const refresh = coalescedRun(run);
38+
39+
const first = refresh();
40+
const second = refresh();
41+
runs[0]!.resolve();
42+
await first;
43+
await vi.waitFor(() => expect(runs).toHaveLength(2));
44+
runs[1]!.reject(new Error("read failed"));
45+
await expect(second).rejects.toThrow("read failed");
46+
});
47+
48+
it("runs once per call when calls do not overlap", async () => {
3249
let count = 0;
3350
const refresh = coalescedRun(async () => {
3451
count += 1;
3552
});
3653
await refresh();
37-
await Promise.resolve();
38-
expect(count).toBe(1);
54+
await refresh();
55+
expect(count).toBe(2);
3956
});
4057
});

‎apps/mobile/src/lib/coalescedRun.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
/**
22
* Runs `run` at most once at a time. A call made while a run is in flight
3-
* returns that run and queues one more after it, so input that arrives
4-
* mid-run (a native share handed over on resume) is still read.
3+
* queues one more run after it and returns that queued run, so input that
4+
* arrives mid-run (a native share handed over on resume) is still read, and
5+
* the caller that asked for it sees how it ended.
56
*/
67
export function coalescedRun(run: () => Promise<void>): () => Promise<void> {
78
let inFlight: Promise<void> | null = null;
8-
let again = false;
9+
let queued: Promise<void> | null = null;
910
const start = (): Promise<void> => {
1011
if (inFlight) {
11-
again = true;
12-
return inFlight;
12+
queued ??= inFlight
13+
.catch(() => undefined)
14+
.then(() => {
15+
queued = null;
16+
return start();
17+
});
18+
return queued;
1319
}
1420
inFlight = run().finally(() => {
1521
inFlight = null;
16-
if (again) {
17-
again = false;
18-
void start();
19-
}
2022
});
2123
return inFlight;
2224
};

0 commit comments

Comments
 (0)