1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-27 12:36:14 +01:00

cleanup requests. dont resolve tabs_message later

This commit is contained in:
Kyle Spearrin 2019-08-19 12:44:05 -04:00
parent 94805468bc
commit ecfeea37e8
2 changed files with 34 additions and 5 deletions

View File

@ -114,7 +114,7 @@ export class BrowserApi {
tab: tab, tab: tab,
obj: JSON.stringify(obj), obj: JSON.stringify(obj),
options: options, options: options,
})); }), true);
/* /*
let t = tab.safariTab; let t = tab.safariTab;
if (!t || !t.page) { if (!t || !t.page) {

View File

@ -1,19 +1,26 @@
import { BrowserApi } from './browserApi'; import { BrowserApi } from './browserApi';
export class SafariApp { export class SafariApp {
static inited = false;
static init() { static init() {
if (SafariApp.inited) {
return;
}
SafariApp.inited = true;
if (BrowserApi.isSafariApi) { if (BrowserApi.isSafariApi) {
(window as any).bitwardenSafariAppRequests = (window as any).bitwardenSafariAppRequests =
new Map<string, { resolve: (value?: unknown) => void, date: Date }>(); new Map<string, { resolve: (value?: unknown) => void, timeoutDate: Date }>();
(window as any).bitwardenSafariAppMessageListeners = (window as any).bitwardenSafariAppMessageListeners =
new Map<string, { resolve: (value?: unknown) => void, date: Date }>(); new Map<string, (message: any, sender: any, response: any) => void>();
(window as any).bitwardenSafariAppMessageReceiver = (message: any) => { (window as any).bitwardenSafariAppMessageReceiver = (message: any) => {
SafariApp.receiveMessageFromApp(message); SafariApp.receiveMessageFromApp(message);
}; };
setInterval(() => SafariApp.cleanupOldRequests(), 5 * 60000);
} }
} }
static sendMessageToApp(command: string, data: any = null): Promise<any> { static sendMessageToApp(command: string, data: any = null, resolveNow = false): Promise<any> {
if (!BrowserApi.isSafariApi) { if (!BrowserApi.isSafariApi) {
return Promise.resolve(null); return Promise.resolve(null);
} }
@ -26,7 +33,14 @@ export class SafariApp {
data: data, data: data,
responseData: null, responseData: null,
})); }));
(window as any).bitwardenSafariAppRequests.set(messageId, { resolve: resolve, date: now }); if (resolveNow) {
resolve();
} else {
(window as any).bitwardenSafariAppRequests.set(messageId, {
resolve: resolve,
timeoutDate: new Date(now.getTime() + 5 * 60000),
});
}
}); });
} }
@ -50,6 +64,21 @@ export class SafariApp {
} else if (message.id != null && (window as any).bitwardenSafariAppRequests.has(message.id)) { } else if (message.id != null && (window as any).bitwardenSafariAppRequests.has(message.id)) {
const p = (window as any).bitwardenSafariAppRequests.get(message.id); const p = (window as any).bitwardenSafariAppRequests.get(message.id);
p.resolve(message.responseData); p.resolve(message.responseData);
(window as any).bitwardenSafariAppRequests.delete(message.id);
} }
} }
private static cleanupOldRequests() {
const remoteIds: string[] = [];
((window as any).bitwardenSafariAppRequests as
Map<string, { resolve: (value?: unknown) => void, timeoutDate: Date }>)
.forEach((v, key) => {
if (v.timeoutDate < new Date()) {
remoteIds.push(key);
}
});
remoteIds.forEach((id) => {
(window as any).bitwardenSafariAppRequests.delete(id);
});
}
} }