Skip to content

Commit ecb03e7

Browse files
committed
fix(overlay): forget a Capacitor page that leaves while a push is on its way
A page hidden before its first push landed sent no forget, because the server had not answered yet, so the page stayed in the panel until it expired. Leaving during a push now forgets the page once that push lands, and resume cancels it.
1 parent af2ca6c commit ecb03e7

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,26 @@ describe('attachCapacitor', () => {
152152
expect(rpc.pushes().at(-1)?.full).toBe(true);
153153
});
154154

155+
it('forgets a page that leaves while its first push is on its way, once the push lands', async () => {
156+
let land!: () => void;
157+
const rpc = fakeRpc();
158+
const call = rpc.my.rpc.call.getMockImplementation()!;
159+
rpc.my.rpc.call.mockImplementation(async (name: string, arg: unknown) => {
160+
if (name === 'push-capacitor' && !land) await new Promise<void>((done) => (land = done));
161+
return call(name, arg);
162+
});
163+
const capacitor = attach(rpc, { Capacitor: fakeCapacitor() });
164+
await vi.advanceTimersByTimeAsync(0);
165+
166+
capacitor.leave();
167+
expect(rpc.calls).toEqual([]);
168+
land();
169+
await vi.advanceTimersByTimeAsync(0);
170+
expect(rpc.calls.map(([name]) => name)).toEqual(['push-capacitor', 'forget-capacitor-page']);
171+
await vi.advanceTimersByTimeAsync(4_000);
172+
expect(rpc.calls).toHaveLength(2);
173+
});
174+
155175
it('restores the bridge when it stops', () => {
156176
const cap = fakeCapacitor();
157177
const original = cap.nativePromise;

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ export function attachCapacitor(my: RpcScope, pageId: string, options: Capacitor
7474
let lastSentAt = 0;
7575
let sent = false;
7676
let left = false;
77+
// A page that leaves while a push is on its way forgets itself once the
78+
// push lands, so that push can't bring the page back.
79+
let forgetAfterPush = false;
7780
let stopped = false;
7881
let disabled = false;
7982
let failures = 0;
@@ -158,12 +161,22 @@ export function attachCapacitor(my: RpcScope, pageId: string, options: Capacitor
158161
} finally {
159162
pushing = false;
160163
}
164+
if (forgetAfterPush) {
165+
forgetAfterPush = false;
166+
if (left) forget();
167+
return;
168+
}
161169
if (again) {
162170
again = false;
163171
schedule(Math.max(0, retryAt - Date.now()));
164172
}
165173
};
166174

175+
const forget = () => {
176+
sent = false;
177+
void my.rpc.call('forget-capacitor-page', pageId).catch(() => {});
178+
};
179+
167180
const stopTicking = () => {
168181
clearInterval(tick);
169182
tick = undefined;
@@ -205,13 +218,14 @@ export function attachCapacitor(my: RpcScope, pageId: string, options: Capacitor
205218
full = true;
206219
clearTimeout(timer);
207220
timer = undefined;
208-
// Only a page the server heard from has anything to forget.
209-
if (sent) void my.rpc.call('forget-capacitor-page', pageId).catch(() => {});
210-
sent = false;
221+
// Only a page the server heard from, or is about to, has anything to forget.
222+
if (pushing) forgetAfterPush = true;
223+
else if (sent) forget();
211224
},
212225
/** After `pageshow`, a page back from the cache reports again. */
213226
resume() {
214227
left = false;
228+
forgetAfterPush = false;
215229
if (attach()) schedule(0);
216230
},
217231
stop() {

0 commit comments

Comments
 (0)