mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-11 18:19:58 +00:00
Restructure background script (#70)
Splits some background script functionality into separate modules: - Receiver selector handling is moved to ./SelectorManager. - Status bridge handling is moved to ./StatusManager. - Menu creation and updates are handled in ./createMenus. - Shim creation is handled in ./createShim. TypedEventTarget allows EventTarget-derived classes to export typed events. Options type definition is moved to ./lib/options, module assumes more responsibility for update handling and provides a "changed" event. Private cast._requestSession method allows bypassing receiver selector.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
import { loadScript } from "../lib/utils";
|
||||
import { Message } from "../types";
|
||||
import { onMessageResponse, sendMessage } from "./eventMessageChannel";
|
||||
|
||||
|
||||
@@ -17,12 +18,17 @@ if (isFramework) {
|
||||
|
||||
|
||||
// Message port to background script
|
||||
const backgroundPort = browser.runtime.connect({ name: "shim" });
|
||||
export const backgroundPort = browser.runtime.connect({ name: "shim" });
|
||||
|
||||
// Forward background messages to shim
|
||||
backgroundPort.onMessage.addListener(sendMessage);
|
||||
const forwardToShim = (message: Message) => sendMessage(message);
|
||||
const forwardToMain = (message: Message) => backgroundPort.postMessage(message);
|
||||
|
||||
// Forward shim messages to background
|
||||
onMessageResponse(message => {
|
||||
backgroundPort.postMessage(message);
|
||||
// Add message listeners
|
||||
backgroundPort.onMessage.addListener(forwardToShim);
|
||||
const listener = onMessageResponse(forwardToMain);
|
||||
|
||||
// Remove listeners
|
||||
backgroundPort.onDisconnect.addListener(() => {
|
||||
backgroundPort.onMessage.removeListener(forwardToShim);
|
||||
listener.disconnect();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user