Document extension messages and remove unnecessary messages/message data

This commit is contained in:
hensm
2022-08-26 21:22:52 +01:00
parent 5b8a55a2de
commit fa953dda63
7 changed files with 44 additions and 38 deletions

View File

@@ -67,7 +67,8 @@ export default class ReceiverSelector extends TypedEventTarget<ReceiverSelectorE
private wasReceiverSelected = false;
private appId?: string;
private pageInfo?: ReceiverSelectorPageInfo;
pageInfo?: ReceiverSelectorPageInfo;
constructor() {
super();

View File

@@ -93,7 +93,7 @@ export default new (class {
instance.contentPort.postMessage({
subject: "cast:initialized",
data: await bridge.getInfo()
data: { isAvailable: (await bridge.getInfo()).isVersionCompatible }
});
return instance;
@@ -197,12 +197,26 @@ export default new (class {
) {
// Intercept messages to store relevant info
switch (message.subject) {
case "cast:sessionCreated":
case "cast:sessionCreated": {
// Close after session is created
const selector = ReceiverSelector.sharedInstance;
if (
selector.isOpen &&
// If selector context is the same as the instance context
selector.pageInfo?.tabId === instance.contentTabId &&
selector.pageInfo?.frameId === instance.contentFrameId &&
// If selector is supposed to close
(await options.get("receiverSelectorWaitForConnection"))
) {
selector.close();
}
instance.session = {
deviceId: message.data.receiverId,
sessionId: message.data.sessionId
};
break;
}
}
instance.contentPort.postMessage(message);
@@ -296,23 +310,6 @@ export default new (class {
break;
}
/**
* TODO: If we're closing a selector, make sure it's the
* same one that caused the session creation.
*/
case "main:closeReceiverSelector": {
const selector = ReceiverSelector.sharedInstance;
const shouldClose = await options.get(
"receiverSelectorWaitForConnection"
);
if (selector.isOpen && shouldClose) {
selector.close();
}
break;
}
}
}

View File

@@ -97,9 +97,7 @@ export function ensureInit(): Promise<TypedMessagePort<Message>> {
function handleIncomingMessageToCast(message: Message) {
switch (message.subject) {
case "cast:initialized": {
initializedBridgeInfo = message.data;
if (initializedBridgeInfo.isVersionCompatible) {
if (message.data.isAvailable) {
resolve(initializedBackgroundPort);
} else {
reject();

View File

@@ -49,7 +49,7 @@ eventMessaging.page.addListener(async message => {
// Call page script/framework API script's init function
const initFn = _window.__onGCastApiAvailable;
if (initFn && typeof initFn === "function") {
initFn(message.data.isVersionCompatible);
initFn(message.data.isAvailable);
}
break;

View File

@@ -143,11 +143,6 @@ export default class {
* and data needed to create cast API objects is sent.
*/
case "cast:sessionCreated": {
// Notify background to close UI
eventMessaging.page.sendMessage({
subject: "main:closeReceiverSelector"
});
const status = message.data;
const receiverDevice = this.#receiverDevices.get(
status.receiverId

View File

@@ -1,7 +1,6 @@
"use strict";
import type { TypedPort } from "./lib/TypedPort";
import type { BridgeInfo } from "./lib/bridge";
import type {
ReceiverSelection,
@@ -39,6 +38,7 @@ import type { ReceiverDevice, ReceiverSelectorMediaType } from "./types";
* components.
*/
type ExtMessageDefinitions = {
/** Initial data to send to selector popup. */
"popup:init": {
appId?: string;
pageInfo?: {
@@ -47,30 +47,49 @@ type ExtMessageDefinitions = {
frameId: number;
};
};
/** Updates selector popup with new data. */
"popup:update": {
receiverDevices: ReceiverDevice[];
defaultMediaType?: ReceiverSelectorMediaType;
availableMediaTypes?: ReceiverSelectorMediaType;
};
"popup:close": undefined;
/**
* Sent from the selector popup when a receiver has been
* selected.
*/
"main:receiverSelected": ReceiverSelection;
/**
* Sent from the selector popup when a receiver has been
* stopped. Used to provide cast API receiver action updates.
*/
"main:receiverStopped": { deviceId: string };
/** Allows the selector popup to send cast NS_RECEIVER messages. */
"main:sendReceiverMessage": ReceiverSelectorReceiverMessage;
/** Allows the selector popup to send cast NS_MEDIA messages. */
"main:sendMediaMessage": ReceiverSelectorMediaMessage;
/**
* Sent from the cast API to trigger receiver selection on session
* request.
*/
"main:selectReceiver": {
sessionRequest: SessionRequest;
};
/** Return message to the cast API when a receiver is selected. */
"cast:selectReceiver/selected": ReceiverSelection;
/** Return message to the cast API when a selection is cancelled. */
"cast:selectReceiver/cancelled": undefined;
/** Sent to the cast API when a receiver app is stopped. */
"cast:receiverStoppedAction": { deviceId: string };
"main:closeReceiverSelector": undefined;
/**
* Tells the cast manager to provide the cast API instance with
* receiver data.
*/
"main:initializeCast": { appId: string };
"cast:initialized": BridgeInfo;
"cast:initialized": { isAvailable: boolean };
"cast:receiverDeviceUp": { receiverDevice: ReceiverDevice };
"cast:receiverDeviceDown": { receiverDeviceId: string };

View File

@@ -152,10 +152,6 @@
pageInfo = message.data.pageInfo;
break;
case "popup:close":
window.close();
break;
case "popup:update": {
if (
message.data.availableMediaTypes !== undefined &&