mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-12 10:39:57 +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:
90
ext/src/lib/bridge.ts
Normal file
90
ext/src/lib/bridge.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
"use strict";
|
||||
|
||||
import semver from "semver";
|
||||
|
||||
import nativeMessaging from "./nativeMessaging";
|
||||
import options from "./options";
|
||||
|
||||
|
||||
async function connect (): Promise<browser.runtime.Port> {
|
||||
const applicationName = await options.get("bridgeApplicationName");
|
||||
const bridgePort = nativeMessaging.connectNative(applicationName);
|
||||
|
||||
bridgePort.onDisconnect.addListener(() => {
|
||||
if (bridgePort.error) {
|
||||
console.error(`${applicationName} disconnected:`
|
||||
, this.bridgePort.error.message);
|
||||
} else {
|
||||
console.info(`${applicationName} disconnected`);
|
||||
}
|
||||
});
|
||||
|
||||
return bridgePort;
|
||||
}
|
||||
|
||||
|
||||
export interface BridgeInfo {
|
||||
name: string;
|
||||
version: string;
|
||||
expectedVersion: string;
|
||||
isVersionExact: boolean;
|
||||
isVersionCompatible: boolean;
|
||||
isVersionOlder: boolean;
|
||||
isVersionNewer: boolean;
|
||||
}
|
||||
|
||||
async function getInfo (): Promise<BridgeInfo> {
|
||||
const applicationName = await options.get("bridgeApplicationName");
|
||||
let applicationVersion: string;
|
||||
|
||||
try {
|
||||
const { version } = browser.runtime.getManifest();
|
||||
|
||||
applicationVersion = await nativeMessaging.sendNativeMessage(
|
||||
applicationName
|
||||
, { subject: "bridge:/getInfo"
|
||||
, data: version });
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the target version is above 0.x.x range, API is stable
|
||||
* and versions with minor or patch level changes should be
|
||||
* compatible.
|
||||
*/
|
||||
const isVersionCompatible =
|
||||
semver.eq(applicationVersion, APPLICATION_VERSION)
|
||||
|| semver.diff(applicationVersion, APPLICATION_VERSION) !== "major"
|
||||
&& semver.major(APPLICATION_VERSION) !== 0;
|
||||
|
||||
const isVersionExact = semver.eq(applicationVersion, APPLICATION_VERSION);
|
||||
const isVersionOlder = semver.lt(applicationVersion, APPLICATION_VERSION);
|
||||
const isVersionNewer = semver.gt(applicationVersion, APPLICATION_VERSION);
|
||||
|
||||
// Print compatibility info to console
|
||||
if (!isVersionCompatible) {
|
||||
console.error(`Expecting ${applicationName} v${APPLICATION_VERSION}, found v${applicationVersion}.`
|
||||
, isVersionOlder
|
||||
? "Try updating the native app to the latest version."
|
||||
: "Try updating the extension to the latest version");
|
||||
}
|
||||
|
||||
return {
|
||||
name: applicationName
|
||||
, version: applicationVersion
|
||||
, expectedVersion: APPLICATION_VERSION
|
||||
|
||||
// Version info
|
||||
, isVersionExact
|
||||
, isVersionCompatible
|
||||
, isVersionOlder
|
||||
, isVersionNewer
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
export default {
|
||||
connect
|
||||
, getInfo
|
||||
};
|
||||
Reference in New Issue
Block a user