Skip to content

Commit 6508108

Browse files
fix(mobile): a resume during a share refresh reads the inbox again
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6e3064f commit 6508108

3 files changed

Lines changed: 76 additions & 16 deletions

File tree

‎apps/mobile/src/features/sharing/IncomingShareProvider.tsx‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { createIncomingSharePayloadReader } from "./incoming-share-native";
2222
import { IncomingShareInbox } from "./incoming-share-inbox";
2323
import { persistComposerAttachmentFile } from "../../lib/composerImages";
2424
import { subscribeAppActive } from "../../lib/appState";
25+
import { coalescedRun } from "../../lib/coalescedRun";
2526
import { rootNavigationRef } from "../../lib/rootNavigation";
2627
import { selectWorkspaceDetail } from "../workspace/workspace-detail-selection";
2728
import { isDraftThreadRoute } from "../threads/thread-route-state";
@@ -196,7 +197,6 @@ interface IncomingShareSnapshot {
196197

197198
let snapshot: IncomingShareSnapshot = { drafts: [], isLoading: true };
198199
const snapshotListeners = new Set<() => void>();
199-
let refreshInFlight: Promise<void> | null = null;
200200
let presentation = EMPTY_INCOMING_SHARE_PRESENTATION_STATE;
201201

202202
function publish(drafts: ReadonlyArray<IncomingShareDraft>) {
@@ -224,22 +224,18 @@ function reportRefreshFailure(cause: unknown) {
224224
]);
225225
}
226226

227-
function refreshIncomingShares(): Promise<void> {
228-
if (refreshInFlight) return refreshInFlight;
227+
// A resume while a refresh runs may carry a native handoff that refresh already
228+
// passed, so it queues one more refresh instead of joining the running one.
229+
const refreshIncomingShares = coalescedRun(async () => {
229230
const enabled = receiveSharingEnabled();
230-
refreshInFlight = (async () => {
231-
try {
232-
publish(await incomingShareInbox.refresh({ ingestNative: enabled }));
233-
} catch (cause) {
234-
const persisted = await incomingShareInbox.refresh({ ingestNative: false }).catch(() => null);
235-
publish(persisted ?? snapshot.drafts);
236-
reportRefreshFailure(cause);
237-
}
238-
})().finally(() => {
239-
refreshInFlight = null;
240-
});
241-
return refreshInFlight;
242-
}
231+
try {
232+
publish(await incomingShareInbox.refresh({ ingestNative: enabled }));
233+
} catch (cause) {
234+
const persisted = await incomingShareInbox.refresh({ ingestNative: false }).catch(() => null);
235+
publish(persisted ?? snapshot.drafts);
236+
reportRefreshFailure(cause);
237+
}
238+
});
243239

244240
/**
245241
* Opens the oldest pending share as a new-task draft unless the user already
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { coalescedRun } from "./coalescedRun";
4+
5+
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+
});
13+
14+
const first = refresh();
15+
const joined = refresh();
16+
refresh();
17+
expect(joined).toBe(first);
18+
expect(runs).toHaveLength(1);
19+
20+
runs[0]!.resolve();
21+
await first;
22+
await Promise.resolve();
23+
expect(runs).toHaveLength(2);
24+
25+
runs[1]!.resolve();
26+
await runs[1]!.promise;
27+
await Promise.resolve();
28+
expect(runs).toHaveLength(2);
29+
});
30+
31+
it("does not run again when nothing was asked during the run", async () => {
32+
let count = 0;
33+
const refresh = coalescedRun(async () => {
34+
count += 1;
35+
});
36+
await refresh();
37+
await Promise.resolve();
38+
expect(count).toBe(1);
39+
});
40+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 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.
5+
*/
6+
export function coalescedRun(run: () => Promise<void>): () => Promise<void> {
7+
let inFlight: Promise<void> | null = null;
8+
let again = false;
9+
const start = (): Promise<void> => {
10+
if (inFlight) {
11+
again = true;
12+
return inFlight;
13+
}
14+
inFlight = run().finally(() => {
15+
inFlight = null;
16+
if (again) {
17+
again = false;
18+
void start();
19+
}
20+
});
21+
return inFlight;
22+
};
23+
return start;
24+
}

0 commit comments

Comments
 (0)