Skip to content

Commit

Permalink
fix(player): dash provider library types should accept namespace
Browse files Browse the repository at this point in the history
ref #1239
  • Loading branch information
mihar-22 committed Apr 22, 2024
1 parent be19840 commit e7e5d65
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
40 changes: 32 additions & 8 deletions packages/vidstack/src/providers/dash/lib-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { DOMEvent, isFunction, isString, isUndefined } from 'maverick.js/std';
import type { MediaContext } from '../../core/api/media-context';
import { coerceToError } from '../../utils/error';
import { loadScript } from '../../utils/network';
import type { DASHConstructor, DASHConstructorLoader, DASHLibrary } from './types';
import type {
DASHConstructor,
DASHConstructorLoader,
DASHLibrary,
DASHNamespace,
DASHNamespaceLoader,
} from './types';

interface LoadDASHConstructorCallbacks {
onLoadStart?: () => void;
Expand Down Expand Up @@ -105,28 +111,38 @@ export class DASHLibLoader {
}

async function importDASH(
loader: DASHConstructor | DASHConstructorLoader | undefined,
loader: DASHConstructor | DASHConstructorLoader | DASHNamespace | DASHNamespaceLoader | undefined,
callbacks: LoadDASHConstructorCallbacks = {},
) {
if (isUndefined(loader)) return undefined;

callbacks.onLoadStart?.();

// Must be static.
if (loader.prototype && loader.prototype !== Function) {
callbacks.onLoaded?.(loader as DASHConstructor);
return loader as DASHConstructor;
if (isDASHConstructor(loader)) {
callbacks.onLoaded?.(loader);
return loader;
}

if (isDASHNamespace(loader)) {
const ctor = loader.MediaPlayer;
callbacks.onLoaded?.(ctor);
return ctor;
}

try {
const ctor = (await (loader as DASHConstructorLoader)())?.default;
const ctor = (await loader())?.default;

if (isDASHNamespace(ctor)) {
callbacks.onLoaded?.(ctor.MediaPlayer);
return ctor.MediaPlayer;
}

if (ctor) {
callbacks.onLoaded?.(ctor);
} else {
throw Error(
__DEV__
? '[vidstack] failed importing `dash.js`. Dynamic import returned invalid constructor.'
? '[vidstack] failed importing `dash.js`. Dynamic import returned invalid object.'
: '',
);
}
Expand Down Expand Up @@ -173,3 +189,11 @@ async function loadDASHScript(

return undefined;
}

function isDASHConstructor(value: any): value is DASHConstructor {
return value && value.prototype && value.prototype !== Function;
}

function isDASHNamespace(value: any): value is DASHNamespace {
return value && 'MediaPlayer' in value;
}
13 changes: 12 additions & 1 deletion packages/vidstack/src/providers/dash/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,16 @@ export { type DASHProviderEvents };

export type DASHConstructor = typeof DASH.MediaPlayer;
export type DASHConstructorLoader = () => Promise<{ default: DASHConstructor } | undefined>;
export type DASHLibrary = DASHConstructor | DASHConstructorLoader | string | undefined;

export type DASHNamespace = typeof DASH;
export type DASHNamespaceLoader = () => Promise<{ default: typeof DASH } | undefined>;

export type DASHLibrary =
| DASHConstructor
| DASHConstructorLoader
| DASHNamespace
| DASHNamespaceLoader
| string
| undefined;

export type DASHInstanceCallback = (player: DASH.MediaPlayerClass) => void;

0 comments on commit e7e5d65

Please sign in to comment.