Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: close popup when tab navigates during connect #807

Merged
merged 4 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/background/services/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ export class Background {
this.sendToPopup.send('SET_TRANSIENT_STATE', state);
});

this.events.on('connect_wallet.close_popup', () => {
this.sendToPopup.send('CLOSE_POPUP', undefined);
});

this.events.on('storage.balance_update', (balance) =>
this.sendToPopup.send('SET_BALANCE', balance),
);
Expand Down
1 change: 1 addition & 0 deletions src/background/services/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface BackgroundEvents {
'open_payments.key_revoked': void;
'open_payments.out_of_funds': void;
'open_payments.invalid_receiver': { tabId: number };
'connect_wallet.close_popup': void;
'storage.rate_of_pay_update': { rate: string };
'storage.state_update': {
state: Storage['state'];
Expand Down
5 changes: 5 additions & 0 deletions src/background/services/openPayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const enum InteractionIntent {
export class OpenPaymentsService {
private browser: Cradle['browser'];
private storage: Cradle['storage'];
private events: Cradle['events'];
private deduplicator: Cradle['deduplicator'];
private logger: Cradle['logger'];
private appName: Cradle['appName'];
Expand All @@ -157,6 +158,7 @@ export class OpenPaymentsService {
constructor({
browser,
storage,
events,
deduplicator,
logger,
t,
Expand All @@ -166,6 +168,7 @@ export class OpenPaymentsService {
Object.assign(this, {
browser,
storage,
events,
deduplicator,
logger,
t,
Expand Down Expand Up @@ -553,6 +556,7 @@ export class OpenPaymentsService {
throw err;
});

this.events.emit('connect_wallet.close_popup');
const { interactRef, hash, tabId } = await this.getInteractionInfo(
grant.interact.redirect,
existingTabId,
Expand Down Expand Up @@ -651,6 +655,7 @@ export class OpenPaymentsService {
browserName: this.browserName,
t: this.t,
});
this.events.emit('connect_wallet.close_popup');
try {
await keyAutoAdd.addPublicKeyToWallet(walletAddress, existingTabId);
return keyAutoAdd.tabId;
Expand Down
6 changes: 0 additions & 6 deletions src/background/services/sendToPopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export class SendToPopup {

private isConnected = false;
private port: Runtime.Port;
private queue = new Map<keyof BackgroundToPopupMessagesMap, any>();

constructor({ browser }: Cradle) {
Object.assign(this, { browser });
Expand All @@ -25,10 +24,6 @@ export class SendToPopup {
}
this.port = port;
this.isConnected = true;
for (const [type, data] of this.queue) {
this.send(type, data);
this.queue.delete(type);
}
port.onDisconnect.addListener(() => {
this.isConnected = false;
});
Expand All @@ -44,7 +39,6 @@ export class SendToPopup {
data: BackgroundToPopupMessagesMap[T],
) {
if (!this.isConnected) {
this.queue.set(type, data);
return;
}
const message = { type, data } as BackgroundToPopupMessage;
Expand Down
2 changes: 2 additions & 0 deletions src/pages/popup/lib/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export const MessageContextProvider = ({
case 'SET_TAB_DATA':
case 'SET_TRANSIENT_STATE':
return dispatch(message);
case 'CLOSE_POPUP':
return window.close();
}
});
port.onDisconnect.addListener(() => {
Expand Down
1 change: 1 addition & 0 deletions src/shared/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export interface BackgroundToPopupMessagesMap {
SET_TAB_DATA: PopupState['tab'];
SET_STATE: { state: Storage['state']; prevState: Storage['state'] };
SET_TRANSIENT_STATE: PopupTransientState;
CLOSE_POPUP: void;
}

export type BackgroundToPopupMessage = {
Expand Down
4 changes: 4 additions & 0 deletions tests/e2e/pages/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export async function openPopup(
await popup.reload({ waitUntil: 'networkidle' });
await popup.waitForSelector('#main', { timeout: 500 });
}

// prevent popup from closing via `window.close()`
popup.exposeFunction('close', () => {});

return popup;
}

Expand Down