Cleanup ShimManager listeners

This commit is contained in:
hensm
2021-04-26 07:53:35 +01:00
parent 8256c9aaba
commit 4cc9b2dd6e
2 changed files with 25 additions and 35 deletions

View File

@@ -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<Shim>();
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 }
});
}
});
}
};

View File

@@ -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();