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

@@ -7,7 +7,7 @@ import semver from "semver";
import { getNextEllipsis
, getWindowCenteredProps } from "../../lib/utils";
import { BridgeInfo } from "../../lib/getBridgeInfo";
import { BridgeInfo } from "../../lib/bridge";
const _ = browser.i18n.getMessage;

View File

@@ -4,13 +4,13 @@
import React, { Component } from "react";
import ReactDOM from "react-dom";
import defaultOptions, { Options } from "../../defaultOptions";
import defaultOptions from "../../defaultOptions";
import Bridge from "./Bridge";
import EditableList from "./EditableList";
import getBridgeInfo, { BridgeInfo } from "../../lib/getBridgeInfo";
import options from "../../lib/options";
import bridge, { BridgeInfo } from "../../lib/bridge";
import options, { Options } from "../../lib/options";
import { REMOTE_MATCH_PATTERN_REGEX } from "../../lib/utils";
import { ReceiverSelectorType } from "../../receiver_selectors";
@@ -88,7 +88,7 @@ class OptionsApp extends Component<{}, OptionsAppState> {
, options: await options.getAll()
});
const bridgeInfo = await getBridgeInfo();
const bridgeInfo = await bridge.getInfo();
const { os } = await browser.runtime.getPlatformInfo();
this.setState({
@@ -350,17 +350,8 @@ class OptionsApp extends Component<{}, OptionsAppState> {
this.form.reportValidity();
try {
const oldOpts = await options.getAll();
await options.setAll(this.state.options);
const alteredOptions = [];
for (const [ key, val ] of Object.entries(this.state.options)) {
const oldVal = oldOpts[key];
if (oldVal !== val) {
alteredOptions.push(key);
}
}
this.setState({
hasSaved: true
}, () => {
@@ -370,12 +361,6 @@ class OptionsApp extends Component<{}, OptionsAppState> {
});
}, 1000);
});
// Send update message / event
browser.runtime.sendMessage({
subject: "optionsUpdated"
, data: { alteredOptions }
});
} catch (err) {
console.error("Failed to save options");
}
@@ -421,7 +406,7 @@ class OptionsApp extends Component<{}, OptionsAppState> {
bridgeLoading: true
});
const bridgeInfo = await getBridgeInfo();
const bridgeInfo = await bridge.getInfo();
this.setState({
bridgeInfo

View File

@@ -179,11 +179,6 @@ class PopupApp extends Component<{}, PopupAppState> {
try {
const filePath = window.prompt();
// Validate URL
const fileUrl = new URL(filePath.startsWith("file://")
? filePath
: `file://${filePath}`);
this.setState({
mediaType
, filePath