1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-07 09:31:31 +01:00

move safari app state to window object

This commit is contained in:
Kyle Spearrin 2019-08-15 16:52:58 -04:00
parent 06234f6b23
commit 7b5e15ce30

View File

@ -3,6 +3,10 @@ import { BrowserApi } from './browserApi';
export class SafariApp {
static init() {
if (BrowserApi.isSafariApi) {
(window as any).bitwardenSafariAppRequests =
new Map<string, { resolve: (value?: unknown) => void, date: Date }>();
(window as any).bitwardenSafariAppMessageListeners =
new Map<string, { resolve: (value?: unknown) => void, date: Date }>();
(window as any).bitwardenSafariAppMessageReceiver = (message: any) => {
SafariApp.receiveMessageFromApp(message);
};
@ -22,26 +26,23 @@ export class SafariApp {
data: data,
responseData: null,
}));
SafariApp.requests.set(messageId, { resolve: resolve, date: now });
(window as any).bitwardenSafariAppRequests.set(messageId, { resolve: resolve, date: now });
});
}
static addMessageListener(name: string, callback: (message: any, sender: any, response: any) => void) {
SafariApp.messageListeners.set(name, callback);
(window as any).bitwardenSafariAppMessageListeners.set(name, callback);
}
static sendMessageToListeners(message: any, sender: any, response: any) {
SafariApp.messageListeners.forEach((f) => f(message, sender, response));
(window as any).bitwardenSafariAppMessageListeners.forEach((f: any) => f(message, sender, response));
}
private static requests = new Map<string, { resolve: (value?: unknown) => void, date: Date }>();
private static messageListeners = new Map<string, (message: any, sender: any, response: any) => void>();
private static receiveMessageFromApp(message: any) {
if (message == null || message.id == null || !SafariApp.requests.has(message.id)) {
if (message == null || message.id == null || !(window as any).bitwardenSafariAppRequests.has(message.id)) {
return;
}
const p = SafariApp.requests.get(message.id);
const p = (window as any).bitwardenSafariAppRequests.get(message.id);
p.resolve(message.responseData);
}
}