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:
Matt Hensman
2019-07-26 00:09:51 +01:00
committed by GitHub
parent 2fe72ed24c
commit ba8c28bf39
40 changed files with 1751 additions and 1241 deletions

View File

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