import { BasePlugin, KalturaPlayer } from '@playkit-js/kaltura-player-js'; import { UnispherePluginConfig } from './types'; import { UnispherePluginManager } from './unisphere-plugin-manager'; import { UnisphereWorkspaceType } from '@unisphere/runtime'; import { fetchAndLoadUnisphereWorkspace, getUnisphereInstance } from '@unisphere/runtime-js'; import { getUniqueId } from './services'; export const pluginName = 'unisphere'; export class UnispherePlugin extends BasePlugin { public static defaultConfig: UnispherePluginConfig = { widgets: [] }; private unispherePluginManager: UnispherePluginManager; private isReady = false; private workspaceName = ''; private shouldCreateWorkspace = false; constructor(name: string, player: KalturaPlayer, config: UnispherePluginConfig) { super(name, player, config); this.unispherePluginManager = new UnispherePluginManager(player, this.eventManager, this.logger); // TODO remove once this is added to providers.env // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const { unisphereLoaderUrl } = this.player.config.provider; if (unisphereLoaderUrl && typeof this.config.workspaceName === 'string') { this.workspaceName = this.config.workspaceName; } else { this.workspaceName = getUniqueId('kaltura_player_workspace_'); this.shouldCreateWorkspace = true; } } private async getWorkspace(): Promise { if (this.shouldCreateWorkspace) { return fetchAndLoadUnisphereWorkspace({ // TODO remove once this is added to providers.env // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore serverUrl: this.player.config.provider.unisphereLoaderUrl, workspaceName: this.workspaceName, ui: { theme: 'light', language: 'en' } }) as unknown as UnisphereWorkspaceType; // TODO remove once there the type is updated } return getUnisphereInstance(this.config.workspaceName); } public static isValid(): boolean { return true; } public loadMedia(): void { if (this.isReady) { this.unispherePluginManager.updateSessionData(); this.unispherePluginManager.loadWidgets(this.config.widgets); } else { this.getWorkspace() .then((workspaceInstance: UnisphereWorkspaceType) => { this.unispherePluginManager.init(workspaceInstance, this.config.widgets); this.unispherePluginManager.updateSessionData(); this.isReady = true; }) .catch(() => { this.logger.warn('Failed to load unisphere workspace', this.workspaceName); }); } } public reset(): void { this.unispherePluginManager.reset(); } public destroy(): void { this.eventManager.destroy(); this.unispherePluginManager.destroy(); } }