Skip to content

Commit

Permalink
fix(Controller logic): Catch all 'Receiving end does not exist' errors
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
  • Loading branch information
marcelklehr committed Jul 18, 2024
1 parent 1daa182 commit 5133823
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
22 changes: 19 additions & 3 deletions src/lib/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
},
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/lib/browser/BrowserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] })
Expand Down

0 comments on commit 5133823

Please sign in to comment.