From 5133823275aa6810604867433e5a6f48372ff73f Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Thu, 18 Jul 2024 16:46:36 +0200 Subject: [PATCH] fix(Controller logic): Catch all 'Receiving end does not exist' errors Signed-off-by: Marcel Klehr --- src/lib/Controller.ts | 22 +++++++++++++++++++--- src/lib/browser/BrowserController.js | 16 ++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/lib/Controller.ts b/src/lib/Controller.ts index 7e87b20ddc..ac2645567a 100644 --- a/src/lib/Controller.ts +++ b/src/lib/Controller.ts @@ -45,10 +45,26 @@ export default class Controller implements IController { if (Capacitor.getPlatform() === 'web') { const browser = (await import('../lib/browser-api')).default return { - postMessage: (data) => browser.runtime.sendMessage(data), + postMessage: (data) => { + try { + browser.runtime.sendMessage(data) + } catch (e) { + console.warn(e) + } + }, addEventListener: (fn) => { - browser.runtime.onMessage.addListener(fn) - return () => browser.runtime.onMessage.removeListener(fn) + try { + browser.runtime.onMessage.addListener(fn) + } catch (e) { + console.warn(e) + } + return () => { + try { + browser.runtime.onMessage.removeListener(fn) + } catch (e) { + console.warn(e) + } + } }, } } diff --git a/src/lib/browser/BrowserController.js b/src/lib/browser/BrowserController.js index 3b86e163da..cdaa908a0f 100644 --- a/src/lib/browser/BrowserController.js +++ b/src/lib/browser/BrowserController.js @@ -141,12 +141,24 @@ export default class BrowserController { if (!navigator.userAgent.includes('Firefox') && typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) { addEventListener('message', (event) => this._receiveEvent(event.data, (data) => event.source.postMessage(data))) } else { - browser.runtime.onMessage.addListener((data) => void (this._receiveEvent(data, (data) => browser.runtime.sendMessage(data)))) + browser.runtime.onMessage.addListener((data) => void (this._receiveEvent(data, (data) => { + try { + browser.runtime.sendMessage(data) + } catch (e) { + console.warn(e) + } + }))) } this.onStatusChange(async() => { if (self?.clients) { const clientList = await self.clients.matchAll() - clientList.forEach(client => client.postMessage({ type: 'status:update', params: [] })) + clientList.forEach(client => { + try { + client.postMessage({ type: 'status:update', params: [] }) + } catch (e) { + console.warn(e) + } + }) } else { try { await browser.runtime.sendMessage({ type: 'status:update', params: [] })