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:
@@ -2,11 +2,14 @@
|
||||
|
||||
import * as cast from "./cast";
|
||||
|
||||
import { BridgeInfo } from "../lib/getBridgeInfo";
|
||||
import { BridgeInfo } from "../lib/bridge";
|
||||
import { Message } from "../types";
|
||||
import { onMessage } from "./eventMessageChannel";
|
||||
|
||||
|
||||
let initializedBridgeInfo: BridgeInfo;
|
||||
let initializedBackgroundPort: browser.runtime.Port;
|
||||
|
||||
/**
|
||||
* To support exporting an API from a module, we need to
|
||||
* retain the event-based message passing despite not
|
||||
@@ -14,17 +17,44 @@ import { onMessage } from "./eventMessageChannel";
|
||||
* for and emits these messages, and changing that behavior
|
||||
* is too messy.
|
||||
*/
|
||||
export function init (): Promise<BridgeInfo> {
|
||||
export function ensureInit (): Promise<browser.runtime.Port> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
|
||||
// Trigger message port setup side-effects
|
||||
import("./contentBridge");
|
||||
// If already initialized, just return existing bridge info
|
||||
if (initializedBridgeInfo) {
|
||||
if (initializedBridgeInfo.isVersionCompatible) {
|
||||
resolve(initializedBackgroundPort);
|
||||
} else {
|
||||
reject();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the module is imported into a background script
|
||||
* context, the location will be the internal extension URL,
|
||||
* whereas in a content script, it will be the content page
|
||||
* URL.
|
||||
*/
|
||||
if (window.location.protocol === "moz-extension:") {
|
||||
//
|
||||
} else {
|
||||
// Trigger message port setup side-effects
|
||||
const { backgroundPort } = await import("./contentBridge");
|
||||
initializedBackgroundPort = backgroundPort;
|
||||
}
|
||||
|
||||
onMessage(message => {
|
||||
switch (message.subject) {
|
||||
case "shim:/initialized": {
|
||||
const bridgeInfo: BridgeInfo = message.data;
|
||||
resolve(bridgeInfo);
|
||||
initializedBridgeInfo = message.data;
|
||||
|
||||
if (initializedBridgeInfo.isVersionCompatible) {
|
||||
resolve(initializedBackgroundPort);
|
||||
} else {
|
||||
reject();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user