From 4cc9b2dd6eeec0eaf074750e06e09cd8308aba77 Mon Sep 17 00:00:00 2001 From: hensm Date: Mon, 26 Apr 2021 07:53:35 +0100 Subject: [PATCH] Cleanup ShimManager listeners --- ext/src/background/ShimManager.ts | 47 ++++++++++++++++--------------- ext/src/background/background.ts | 13 --------- 2 files changed, 25 insertions(+), 35 deletions(-) diff --git a/ext/src/background/ShimManager.ts b/ext/src/background/ShimManager.ts index 1e0f5c2..64dbb9f 100644 --- a/ext/src/background/ShimManager.ts +++ b/ext/src/background/ShimManager.ts @@ -3,7 +3,7 @@ import bridge from "../lib/bridge"; import loadSender from "../lib/loadSender"; import logger from "../lib/logger"; -import { Message, Port } from "../messaging"; +import messaging, { Message, Port } from "../messaging"; import options from "../lib/options"; import { ReceiverSelectionActionType @@ -30,7 +30,30 @@ export default new class ShimManager { private activeShims = new Set(); public async init() { - await this.initStatusListeners(); + // Wait for "shim" ports + messaging.onConnect.addListener(async port => { + if (port.name === "shim") { + this.createShim(port); + } + }); + + StatusManager.addEventListener("serviceUp", ev => { + for (const shim of this.activeShims) { + shim.contentPort.postMessage({ + subject: "shim:serviceUp" + , data: { id: ev.detail.id } + }); + } + }); + + StatusManager.addEventListener("serviceDown", ev => { + for (const shim of this.activeShims) { + shim.contentPort.postMessage({ + subject: "shim:serviceDown" + , data: { id: ev.detail.id } + }); + } + }); } public getShim(tabId: number, frameId?: number) { @@ -244,24 +267,4 @@ export default new class ShimManager { } } } - - private async initStatusListeners() { - StatusManager.addEventListener("serviceUp", ev => { - for (const shim of this.activeShims) { - shim.contentPort.postMessage({ - subject: "shim:serviceUp" - , data: { id: ev.detail.id } - }); - } - }); - - StatusManager.addEventListener("serviceDown", ev => { - for (const shim of this.activeShims) { - shim.contentPort.postMessage({ - subject: "shim:serviceDown" - , data: { id: ev.detail.id } - }); - } - }); - } }; diff --git a/ext/src/background/background.ts b/ext/src/background/background.ts index f563ab1..1728feb 100755 --- a/ext/src/background/background.ts +++ b/ext/src/background/background.ts @@ -175,7 +175,6 @@ async function init() { } const selection = await ReceiverSelectorManager.getSelection(tab.id); - if (selection) { loadSender({ tabId: tab.id @@ -184,18 +183,6 @@ async function init() { }); } }); - - - /** - * When a message port connection with the name "shim" is - * established, pass it to createShim to handle the setup - * and maintenance. - */ - messaging.onConnect.addListener(async port => { - if (port.name === "shim") { - ShimManager.createShim(port); - } - }); } init();