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:
@@ -1,15 +1,19 @@
|
||||
"use strict";
|
||||
|
||||
import options from "../lib/options";
|
||||
import cast, { init } from "../shim/export";
|
||||
import cast, { ensureInit } from "../shim/export";
|
||||
|
||||
import { ReceiverSelectorMediaType }
|
||||
from "../receiver_selectors/ReceiverSelector";
|
||||
|
||||
import { Receiver } from "../types";
|
||||
|
||||
|
||||
// Variables passed from background
|
||||
const { selectedMedia }
|
||||
: { selectedMedia: ReceiverSelectorMediaType } = (window as any);
|
||||
const { selectedMedia
|
||||
, selectedReceiver }
|
||||
: { selectedMedia: ReceiverSelectorMediaType
|
||||
, selectedReceiver: Receiver } = (window as any);
|
||||
|
||||
|
||||
const FX_CAST_RECEIVER_APP_NAMESPACE = "urn:x-cast:fx_cast";
|
||||
@@ -42,6 +46,10 @@ if (typeof navigator.mediaDevices.getDisplayMedia === "undefined") {
|
||||
* receiver device.
|
||||
*/
|
||||
function sendAppMessage (subject: string, data: any) {
|
||||
if (!session) {
|
||||
return;
|
||||
}
|
||||
|
||||
session.sendMessage(FX_CAST_RECEIVER_APP_NAMESPACE, {
|
||||
subject
|
||||
, data
|
||||
@@ -54,9 +62,7 @@ window.addEventListener("beforeunload", () => {
|
||||
});
|
||||
|
||||
|
||||
async function onRequestSessionSuccess (
|
||||
newSession: cast.Session
|
||||
, newSelectedMedia: ReceiverSelectorMediaType) {
|
||||
async function onRequestSessionSuccess (newSession: cast.Session) {
|
||||
|
||||
cast.logMessage("onRequestSessionSuccess");
|
||||
|
||||
@@ -83,7 +89,7 @@ async function onRequestSessionSuccess (
|
||||
sendAppMessage("iceCandidate", ev.candidate);
|
||||
});
|
||||
|
||||
switch (newSelectedMedia) {
|
||||
switch (selectedMedia) {
|
||||
case ReceiverSelectorMediaType.Tab: {
|
||||
const canvas = document.createElement("canvas");
|
||||
const ctx = canvas.getContext("2d");
|
||||
@@ -161,8 +167,9 @@ function receiverListener (availability: string) {
|
||||
|
||||
if (availability === cast.ReceiverAvailability.AVAILABLE) {
|
||||
wasSessionRequested = true;
|
||||
cast.requestSession(
|
||||
onRequestSessionSuccess
|
||||
cast._requestSession(
|
||||
selectedReceiver
|
||||
, onRequestSessionSuccess
|
||||
, onRequestSessionError);
|
||||
}
|
||||
}
|
||||
@@ -182,13 +189,7 @@ function onInitializeError () {
|
||||
}
|
||||
|
||||
|
||||
init().then(async bridgeInfo => {
|
||||
if (!bridgeInfo.isVersionCompatible) {
|
||||
console.error("__onGCastApiAvailable error");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
ensureInit().then(async () => {
|
||||
const mirroringAppId = await options.get("mirroringAppId");
|
||||
const sessionRequest = new cast.SessionRequest(mirroringAppId);
|
||||
|
||||
@@ -196,9 +197,7 @@ init().then(async bridgeInfo => {
|
||||
sessionRequest
|
||||
, sessionListener
|
||||
, receiverListener
|
||||
, undefined, undefined
|
||||
, selectedMedia
|
||||
, availableMediaTypes);
|
||||
, undefined, undefined);
|
||||
|
||||
cast.initialize(apiConfig
|
||||
, onInitializeSuccess
|
||||
|
||||
Reference in New Issue
Block a user