make wallet notifications work again

This commit is contained in:
Florian Dold 2020-05-04 17:41:22 +05:30
parent 42b6c58db8
commit cd9a34cbb5
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
2 changed files with 22 additions and 2 deletions

View File

@ -896,6 +896,5 @@ function WalletPopup(): JSX.Element {
} }
export function createPopup(): JSX.Element { export function createPopup(): JSX.Element {
//chrome.runtime.connect({ name: "popup" });
return <WalletPopup />; return <WalletPopup />;
} }

View File

@ -68,6 +68,8 @@ const extendedPermissions = {
origins: ["http://*/*", "https://*/*"], origins: ["http://*/*", "https://*/*"],
}; };
const notificationPorts: chrome.runtime.Port[] = [];
async function handleMessage( async function handleMessage(
sender: MessageSender, sender: MessageSender,
type: MessageType, type: MessageType,
@ -447,6 +449,15 @@ async function reinitWallet(): Promise<void> {
http, http,
new BrowserCryptoWorkerFactory(), new BrowserCryptoWorkerFactory(),
); );
wallet.addNotificationListener((x) => {
for (const x of notificationPorts) {
try {
x.postMessage({ type: "notification" });
} catch (e) {
console.error(e);
}
}
});
wallet.runRetryLoop().catch((e) => { wallet.runRetryLoop().catch((e) => {
console.log("error during wallet retry loop", e); console.log("error during wallet retry loop", e);
}); });
@ -620,8 +631,18 @@ export async function wxMain(): Promise<void> {
return true; return true;
}); });
chrome.runtime.onConnect.addListener((port) => {
notificationPorts.push(port);
port.onDisconnect.addListener((discoPort) => {
const idx = notificationPorts.indexOf(discoPort);
if (idx >= 0) {
notificationPorts.splice(idx, 1);
}
});
});
setupHeaderListener(); setupHeaderListener();
chrome.permissions.onAdded.addListener((perm) => { chrome.permissions.onAdded.addListener((perm) => {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError); console.error(chrome.runtime.lastError);