import { Runtime } from '@jolibox/types'; import { BaseSDK } from './sdk'; export enum JoliboxRuntimeEvents { /** * Event dispatched when the loading starts */ LOAD_START = 'JOLIBOX_RUNTIME_LOAD_START', /** * Event dispatched when the loading is finished */ LOAD_FINISHED = 'JOLIBOX_RUNTIME_LOAD_FINISHED', /** * Event dispatched when the loading progress is updated */ LOAD_PROGRESS = 'JOLIBOX_RUNTIME_LOAD_PROGRESS', /** * Event dispatched when the game TTI (Time to Interactive) is reported */ GAME_TTI = 'JOLIBOX_RUNTIME_GAME_TTI' } /** * @public * Default implementation of JoliboxRuntime, in case the JoliboxRuntime is not available */ export class FallbackJoliboxRuntime extends BaseSDK implements Runtime { /** * Notify the end of the loading, will close the loading splash screen */ loadFinished = (): void => { const loadFinishedEvent = new Event(JoliboxRuntimeEvents.LOAD_FINISHED); window.dispatchEvent(loadFinishedEvent); this.commands.executeCommand('RuntimeSDK.loadFinishedEvent'); }; /** * Notify the progress of the loading, will update the loading splash screen * @param progress - The progress of the loading, should be an integer between 0 and 100 */ notifyLoadProgress = (progress: number): void => { const detail = Math.ceil(progress); const event = new CustomEvent(JoliboxRuntimeEvents.LOAD_PROGRESS, { detail }); window.dispatchEvent(event); this.commands.executeCommand('RuntimeSDK.loadProgressEvent', detail); }; /** * Report the game TTI (Time to Interactive) * @param duration - The duration in milliseconds from game start to interactive */ gameTTI = (): void => { const event = new CustomEvent(JoliboxRuntimeEvents.GAME_TTI, {}); window.dispatchEvent(event); this.commands.executeCommand('RuntimeSDK.gameTTIEvent'); }; } declare global { interface Window { JoliboxRuntime: typeof FallbackJoliboxRuntime; } } //eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore export const RuntimeSDK = window.JoliboxRuntime || FallbackJoliboxRuntime;