Skip to content

Commit 0a29316

Browse files
fix(mobile): a host's on-screen approval prompt can show again after it reconnects
Dropping a host now also forgets the prompt it has on screen, so a later answer does not requeue it and the host can offer the request again once it is back. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent eea7b35 commit 0a29316

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

‎apps/mobile/src/features/connection/connectDeviceApproval.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ describe("createApprovalPromptQueue", () => {
7171
expect(shown).toEqual(["a", "a"]);
7272
});
7373

74+
it("offers a dropped host's on-screen prompt again once the host is back", () => {
75+
const shown: string[] = [];
76+
const answers: Array<(outcome: "done" | "retry" | "later") => void> = [];
77+
const queue = createApprovalPromptQueue<{ pairingLinkId: string; host: string }>(
78+
(prompt, answer) => {
79+
shown.push(prompt.pairingLinkId);
80+
answers.push(answer);
81+
},
82+
);
83+
84+
queue.offer([{ pairingLinkId: "a", host: "mini" }]);
85+
queue.drop((prompt) => prompt.host === "mini");
86+
answers.at(-1)!("retry");
87+
expect(shown).toEqual(["a"]);
88+
queue.offer([{ pairingLinkId: "a", host: "mini" }]);
89+
expect(shown).toEqual(["a", "a"]);
90+
});
91+
7492
it("drops a host's waiting prompts so they cannot hold up other hosts", () => {
7593
const shown: string[] = [];
7694
const answers: Array<(outcome: "done" | "retry" | "later") => void> = [];

‎apps/mobile/src/features/connection/connectDeviceApproval.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,31 @@ export function createApprovalPromptQueue<P extends { readonly pairingLinkId: st
4646
) {
4747
const known = new Set<string>();
4848
const waiting: P[] = [];
49-
let showing = false;
49+
let showing: { readonly prompt: P; dropped: boolean } | null = null;
5050
const next = () => {
5151
if (showing) return;
5252
const prompt = waiting.shift();
5353
if (prompt === undefined) return;
54-
showing = true;
54+
const current = { prompt, dropped: false };
55+
showing = current;
5556
let answered = false;
5657
present(prompt, (outcome) => {
5758
if (answered) return;
5859
answered = true;
59-
showing = false;
60-
if (outcome !== "done") waiting.unshift(prompt);
60+
showing = null;
61+
// A dropped prompt is forgotten, so its host can offer it again when it comes back.
62+
if (outcome !== "done" && !current.dropped) waiting.unshift(prompt);
6163
if (outcome !== "later") next();
6264
});
6365
};
6466
return {
6567
known: known as ReadonlySet<string>,
6668
/** Forgets waiting prompts that match, such as a host that went away. */
6769
drop(matches: (prompt: P) => boolean) {
70+
if (showing && matches(showing.prompt)) {
71+
showing.dropped = true;
72+
known.delete(showing.prompt.pairingLinkId);
73+
}
6874
for (let index = waiting.length - 1; index >= 0; index--) {
6975
const prompt = waiting[index]!;
7076
if (!matches(prompt)) continue;

0 commit comments

Comments
 (0)