Skip to content

Commit

Permalink
fix: chrome.cast.isAvailable fatal typeerror (#15)
Browse files Browse the repository at this point in the history
* fix: chrome.cast.isAvailable fatal typeerror

* fix: simplify logic, debug log instead of warn
  • Loading branch information
luwes authored May 17, 2024
1 parent 4c7a381 commit 8a7862f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
13 changes: 9 additions & 4 deletions packages/castable-video/castable-remote-playback.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
InvalidStateError,
NotSupportedError,
onCastApiAvailable,
isChromeCastAvailable,
castContext,
currentSession,
currentMedia,
Expand All @@ -19,8 +18,14 @@ const castElementRef = new WeakSet();

let cf;

onCastApiAvailable((isAvailable) => {
if (isAvailable && !cf) {
onCastApiAvailable(() => {
if (!globalThis.chrome?.cast?.isAvailable) {
// Useful to see in verbose logs if this shows undefined or false.
console.debug('chrome.cast.isAvailable', globalThis.chrome?.cast?.isAvailable);
return;
}

if (!cf) {
cf = cast.framework;

castContext().addEventListener(cf.CastContextEventType.CAST_STATE_CHANGED, (e) => {
Expand Down Expand Up @@ -109,7 +114,7 @@ export class RemotePlayback extends EventTarget {
throw new InvalidStateError('disableRemotePlayback attribute is present.');
}

if (!isChromeCastAvailable()) {
if (!globalThis.chrome?.cast?.isAvailable) {
throw new NotSupportedError('The RemotePlayback API is disabled on this platform.');
}

Expand Down
27 changes: 8 additions & 19 deletions packages/castable-video/castable-utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global chrome, cast, WeakRef */
/* global WeakRef */

export const privateProps = new WeakMap();

Expand All @@ -21,22 +21,22 @@ export const IterableWeakSet = globalThis.WeakRef ?
} : Set;

export function onCastApiAvailable(callback) {
if (!isChromeCastAvailable()) {
if (!globalThis.chrome?.cast?.isAvailable) {
globalThis.__onGCastApiAvailable = () => {
// The globalThis.__onGCastApiAvailable callback alone is not reliable for
// the added cast.framework. It's loaded in a separate JS file.
// https://www.gstatic.com/eureka/clank/101/cast_sender.js
// https://www.gstatic.com/cast/sdk/libs/sender/1.0/cast_framework.js
customElements
.whenDefined('google-cast-button')
.then(() => callback(chrome.cast.isAvailable));
.then(callback);
};
} else if (!isCastFrameworkAvailable()) {
} else if (!globalThis.cast?.framework) {
customElements
.whenDefined('google-cast-button')
.then(() => callback(chrome.cast.isAvailable));
.then(callback);
} else {
callback(chrome.cast.isAvailable);
callback();
}
}

Expand All @@ -47,26 +47,15 @@ export function requiresCastFramework() {

export function loadCastFramework() {
const sdkUrl = 'https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1';
if (globalThis.chrome.cast || document.querySelector(`script[src="${sdkUrl}"]`)) return;
if (globalThis.chrome?.cast || document.querySelector(`script[src="${sdkUrl}"]`)) return;

const script = document.createElement('script');
script.src = sdkUrl;
document.head.append(script);
}

export function isChromeCastAvailable() {
return typeof chrome !== 'undefined' && chrome.cast && chrome.cast.isAvailable;
}

export function isCastFrameworkAvailable() {
return typeof cast !== 'undefined' && cast.framework;
}

export function castContext() {
if (isCastFrameworkAvailable()) {
return cast.framework.CastContext.getInstance();
}
return undefined;
return globalThis.cast?.framework?.CastContext.getInstance();
}

export function currentSession() {
Expand Down

0 comments on commit 8a7862f

Please sign in to comment.