Skip to content

Commit 844ca9e

Browse files
committed
fix(overlay): send a Capacitor push only after a forget on its way lands
Separate RPC calls may reach the server out of order. A page restored from the back-forward cache could push before the forget it sent on leaving had landed, and the late forget then dropped the page it had just reported. A push now waits for that forget first.
1 parent ecb03e7 commit 844ca9e

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎packages/devtools/src/__tests__/capacitor-overlay.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,30 @@ describe('attachCapacitor', () => {
172172
expect(rpc.calls).toHaveLength(2);
173173
});
174174

175+
it('waits for a forget on its way before it reports again after pageshow', async () => {
176+
let forgotten!: () => void;
177+
const rpc = fakeRpc();
178+
const call = rpc.my.rpc.call.getMockImplementation()!;
179+
rpc.my.rpc.call.mockImplementation(async (name: string, arg: unknown) => {
180+
if (name === 'forget-capacitor-page') await new Promise<void>((done) => (forgotten = done));
181+
return call(name, arg);
182+
});
183+
const capacitor = attach(rpc, { Capacitor: fakeCapacitor() });
184+
await vi.advanceTimersByTimeAsync(0);
185+
186+
capacitor.leave();
187+
capacitor.resume();
188+
await vi.advanceTimersByTimeAsync(0);
189+
expect(rpc.pushes()).toHaveLength(1);
190+
forgotten();
191+
await vi.advanceTimersByTimeAsync(0);
192+
expect(rpc.calls.map(([name]) => name)).toEqual([
193+
'push-capacitor',
194+
'forget-capacitor-page',
195+
'push-capacitor',
196+
]);
197+
});
198+
175199
it('restores the bridge when it stops', () => {
176200
const cap = fakeCapacitor();
177201
const original = cap.nativePromise;

‎packages/devtools/src/capacitor-overlay.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ export function attachCapacitor(my: RpcScope, pageId: string, options: Capacitor
7777
// A page that leaves while a push is on its way forgets itself once the
7878
// push lands, so that push can't bring the page back.
7979
let forgetAfterPush = false;
80+
// Separate RPC calls may arrive out of order, so a push waits for a forget
81+
// still on its way, or the server could drop the page it just got.
82+
let forgetting: Promise<unknown> | undefined;
8083
let stopped = false;
8184
let disabled = false;
8285
let failures = 0;
@@ -133,6 +136,7 @@ export function attachCapacitor(my: RpcScope, pageId: string, options: Capacitor
133136
};
134137
pushing = true;
135138
try {
139+
if (forgetting) await forgetting;
136140
const answer = (await outsideAngular(() => my.rpc.call('push-capacitor', report))) as
137141
{ known?: boolean } | null | undefined;
138142
lastSentAt = Date.now();
@@ -174,7 +178,13 @@ export function attachCapacitor(my: RpcScope, pageId: string, options: Capacitor
174178

175179
const forget = () => {
176180
sent = false;
177-
void my.rpc.call('forget-capacitor-page', pageId).catch(() => {});
181+
const done: Promise<unknown> = my.rpc
182+
.call('forget-capacitor-page', pageId)
183+
.catch(() => {})
184+
.finally(() => {
185+
if (forgetting === done) forgetting = undefined;
186+
});
187+
forgetting = done;
178188
};
179189

180190
const stopTicking = () => {

0 commit comments

Comments
 (0)