mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-12 02:29:59 +00:00
Fix SDK initialization timing issues
This commit is contained in:
@@ -169,7 +169,7 @@ const castManager = new (class {
|
||||
this.activeInstances.add(instance);
|
||||
|
||||
instance.contentPort.postMessage({
|
||||
subject: "cast:initialized",
|
||||
subject: "cast:instanceCreated",
|
||||
data: { isAvailable: (await bridge.getInfo()).isVersionCompatible }
|
||||
});
|
||||
|
||||
@@ -349,7 +349,7 @@ const castManager = new (class {
|
||||
}
|
||||
|
||||
switch (message.subject) {
|
||||
case "main:initializeCast":
|
||||
case "main:initializeCastSdk":
|
||||
instance.apiConfig = message.data.apiConfig;
|
||||
instance.contentPort.postMessage({
|
||||
subject: "cast:receiverAvailabilityUpdated",
|
||||
|
||||
@@ -32,7 +32,7 @@ if (document.currentScript) {
|
||||
|
||||
pageMessenging.page.addListener(async message => {
|
||||
switch (message.subject) {
|
||||
case "cast:initialized": {
|
||||
case "cast:instanceCreated": {
|
||||
// If framework API is loading, wait until completed
|
||||
await frameworkScriptPromise;
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ export function ensureInit(contextTabId?: number): Promise<CastPort> {
|
||||
// castManager -> cast instance
|
||||
managerPort.addEventListener("message", ev => {
|
||||
const message = ev.data as Message;
|
||||
if (message.subject === "cast:initialized") {
|
||||
if (message.subject === "cast:instanceCreated") {
|
||||
if (message.data.isAvailable) {
|
||||
resolve(existingPort);
|
||||
} else {
|
||||
@@ -91,7 +91,7 @@ export function ensureInit(contextTabId?: number): Promise<CastPort> {
|
||||
backgroundPort.onMessage.addListener(function onManagerMessage(
|
||||
message: Message
|
||||
) {
|
||||
if (message.subject === "cast:initialized") {
|
||||
if (message.subject === "cast:instanceCreated") {
|
||||
if (message.data.isAvailable) {
|
||||
resolve(pageMessenging.page.messagePort);
|
||||
} else {
|
||||
|
||||
@@ -54,8 +54,10 @@ export default class {
|
||||
#apiConfig?: ApiConfig;
|
||||
#sessionRequest?: SessionRequest;
|
||||
|
||||
#isInitialized = false;
|
||||
|
||||
/** Current receiver availability. */
|
||||
#receiverAvailability = ReceiverAvailability.UNAVAILABLE;
|
||||
#receiverAvailability?: ReceiverAvailability;
|
||||
|
||||
#initializeSuccessCallback?: () => void;
|
||||
|
||||
@@ -105,10 +107,37 @@ export default class {
|
||||
|
||||
#onMessage(message: Message) {
|
||||
switch (message.subject) {
|
||||
case "cast:initialized":
|
||||
case "cast:instanceCreated":
|
||||
this.isAvailable = true;
|
||||
this.#initializeSuccessCallback?.();
|
||||
this.#apiConfig?.receiverListener(this.#receiverAvailability);
|
||||
break;
|
||||
|
||||
case "cast:receiverAvailabilityUpdated": {
|
||||
/**
|
||||
* The first availability update happens after
|
||||
* initialize is called.
|
||||
*/
|
||||
if (!this.#isInitialized) {
|
||||
this.#isInitialized = true;
|
||||
this.#initializeSuccessCallback?.();
|
||||
}
|
||||
|
||||
const availability = message.data.isAvailable
|
||||
? ReceiverAvailability.AVAILABLE
|
||||
: ReceiverAvailability.UNAVAILABLE;
|
||||
|
||||
// If availability has changed, call receiver listeners
|
||||
if (availability !== this.#receiverAvailability) {
|
||||
this.#receiverAvailability = availability;
|
||||
this.#apiConfig?.receiverListener(availability);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "cast:receiverAction":
|
||||
for (const actionListener of this.#receiverActionListeners) {
|
||||
actionListener(message.data.receiver, message.data.action);
|
||||
}
|
||||
break;
|
||||
|
||||
// Popup closed before session established
|
||||
@@ -127,6 +156,7 @@ export default class {
|
||||
* and data needed to create cast API objects is sent.
|
||||
*/
|
||||
case "cast:sessionCreated": {
|
||||
this.#sessionRequest = undefined;
|
||||
const status = message.data;
|
||||
|
||||
status.receiver.volume = status.volume;
|
||||
@@ -248,26 +278,6 @@ export default class {
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "cast:receiverAvailabilityUpdated": {
|
||||
const availability = message.data.isAvailable
|
||||
? ReceiverAvailability.AVAILABLE
|
||||
: ReceiverAvailability.UNAVAILABLE;
|
||||
|
||||
// If availability has changed, call receiver listeners
|
||||
if (availability !== this.#receiverAvailability) {
|
||||
this.#receiverAvailability = availability;
|
||||
this.#apiConfig?.receiverListener(availability);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "cast:receiverAction":
|
||||
for (const actionListener of this.#receiverActionListeners) {
|
||||
actionListener(message.data.receiver, message.data.action);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,7 +301,7 @@ export default class {
|
||||
}
|
||||
|
||||
pageMessenging.page.sendMessage({
|
||||
subject: "main:initializeCast",
|
||||
subject: "main:initializeCastSdk",
|
||||
data: { apiConfig: this.#apiConfig }
|
||||
});
|
||||
}
|
||||
|
||||
@@ -66,10 +66,19 @@ type ExtMessageDefinitions = {
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Tells the cast manager to provide the cast API instance with
|
||||
* receiver data.
|
||||
*/
|
||||
"main:initializeCastSdk": { apiConfig: ApiConfig };
|
||||
"cast:initialized": { isAvailable: boolean };
|
||||
|
||||
/**
|
||||
* Sent to the cast API when a session is requested or stopped via
|
||||
* the extension UI.
|
||||
*/
|
||||
"cast:receiverAction": { receiver: Receiver; action: ReceiverAction };
|
||||
|
||||
/**
|
||||
* Sent from the cast API to trigger receiver selection on session
|
||||
@@ -81,23 +90,16 @@ type ExtMessageDefinitions = {
|
||||
/** Return message to the cast API when a selection is cancelled. */
|
||||
"cast:sessionRequestCancelled": undefined;
|
||||
|
||||
/**
|
||||
* Sent to the cast API when a session is requested or stopped via
|
||||
* the extension UI.
|
||||
*/
|
||||
"cast:receiverAction": { receiver: Receiver; action: ReceiverAction };
|
||||
|
||||
/**
|
||||
* Tells the cast manager to provide the cast API instance with
|
||||
* receiver data.
|
||||
*/
|
||||
"main:initializeCast": { apiConfig: ApiConfig };
|
||||
"cast:initialized": { isAvailable: boolean };
|
||||
"cast:instanceCreated": { isAvailable: boolean };
|
||||
"cast:receiverAvailabilityUpdated": { isAvailable: boolean };
|
||||
|
||||
"cast:sessionCreated": CastSessionCreatedDetails & { receiver: Receiver };
|
||||
"cast:sessionUpdated": CastSessionUpdatedDetails;
|
||||
|
||||
"cast:receiverAvailabilityUpdated": { isAvailable: boolean };
|
||||
/** 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;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user