All files / app-play/src main.ts

0% Statements 0/46
0% Branches 0/12
0% Functions 0/8
0% Lines 0/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123                                                                                                                                                                                                                                                     
import { onPlayerLangChange } from './player-copy.ts';
import { defineServiceUI } from '@be-music/service-web-theme/client';
import { render } from 'lit';
import {
  configureBGAVideoWorkers,
  configureBrowserWorkers,
  setBrowserWorkerCount,
  disposeBrowserWorkers,
} from '@be-music/player-browser/compute';
import { migratePlayerStorage } from './player-storage.ts';
import { readWorkerCount } from './worker-settings.ts';
import { optionSettingsSync } from './option-settings-sync.ts';
// The player's bootstrap: the page's markup, the browser check, the device classes and the Help modal, then the app
// (`app/app.ts`) once the UI fonts are ready, with the test hook installed over it before it starts.
import { checkBrowserCompat } from '@be-music/player-browser/collection';
import { PlayerWebApp } from './app/app.ts';
import { renderStartAccountLoading } from './leaderboard-ui.ts';
import { wireHelpModal, applyPlayerLanguage } from './help-modal.ts';
import { wireInstallButton } from './install-button.ts';
import { installDialogShortcuts } from './ui/dialog-shortcuts.ts';
import { bindPlayerHeader, mountPlayerHeader } from './player-header.ts';
import { PLAYER_APP_TEMPLATE } from './dom-template.ts';
import { renderBrowserCompatPanel } from './compat-panel.ts';
import { readDeviceSignals, resolveDeviceProfile } from './device-profile.ts';
import { waitForDefaultUIFonts } from './services/built-in-skins.ts';
import { isTestHookEnabled, TestHookRecorder } from './test-hook.ts';
import { liveTestHookState } from './services/test-hook.ts';
import { registerServiceWorker, shouldRegisterServiceWorker } from './service-worker/register.ts';
import './styles.css';
 
let workerStorage: Storage | undefined;
try {
  workerStorage = globalThis.localStorage;
} catch {
  /* Storage can be disabled. */
}
migratePlayerStorage(workerStorage);
setBrowserWorkerCount(readWorkerCount(workerStorage, navigator.hardwareConcurrency));
configureBrowserWorkers(() => new Worker(new URL('./compute-worker.ts', import.meta.url), { type: 'module' }));
// A BGA video the browser can't play streams through these while ffmpeg.wasm transcodes it.
configureBGAVideoWorkers({
  createDecodeWorker: () => new Worker(new URL('./bga-decode-worker.ts', import.meta.url), { type: 'module' }),
  createEncodeWorker: () => new Worker(new URL('./bga-encode-worker.ts', import.meta.url), { type: 'module' }),
});
window.addEventListener('pagehide', (event) => {
  void optionSettingsSync.flush();
  if (!event.persisted) disposeBrowserWorkers();
});
 
const app = document.querySelector<HTMLDivElement>('#app');
if (!app) {
  throw new Error('missing #app');
}
 
defineServiceUI();
render(PLAYER_APP_TEMPLATE, app);
renderStartAccountLoading(app);
 
// Browser compatibility probe runs first so the drop card paints the readiness verdict immediately on first render. We
// do this BEFORE constructing the player app so that even a hard-fail (Pixi `Application.init()` throwing on a no-WebGL2
// browser) still leaves the user looking at the unsupported-browser message rather than a blank canvas.
const browserCompat = checkBrowserCompat();
renderBrowserCompatPanel(browserCompat);
onPlayerLangChange(() => renderBrowserCompatPanel(browserCompat));
 
// Phones and tablets: lighter chrome over the stage, touch play controls, and a rotate prompt on phones held upright
// (see the `html.touch-first` / `html.phone` rules in styles.css).
const deviceProfile = resolveDeviceProfile(readDeviceSignals());
document.documentElement.classList.add('dark');
document.documentElement.classList.toggle('touch-first', deviceProfile.touchFirst);
document.documentElement.classList.toggle('phone', deviceProfile.phone);
 
// Wire up the bottom-right Help button + unified Help / OSS modal. The acknowledgement list is rendered lazily on first
// open of the OSS tab, so the initial paint isn't blocked on rendering ~30 dependency cards.
const header = document.querySelector<HTMLElement>('#player-service-header');
if (header !== null) {
  mountPlayerHeader(header);
  bindPlayerHeader({ selectLanguage: applyPlayerLanguage });
}
wireHelpModal();
wireInstallButton();
installDialogShortcuts();
 
// The installable app's offline shell (`service-worker/`): production builds only, and never under the test hook.
if (
  shouldRegisterServiceWorker({
    production: import.meta.env.PROD,
    testHook: isTestHookEnabled(window.location.href, import.meta.env.DEV),
    supported: 'serviceWorker' in navigator,
  })
) {
  registerServiceWorker(navigator.serviceWorker);
}
 
void waitForDefaultUIFonts()
  .then(() => optionSettingsSync.reconcile())
  .finally(() => {
    setBrowserWorkerCount(readWorkerCount(workerStorage, navigator.hardwareConcurrency));
    // `window.__beMusicTest` for the end-to-end suite (`?e2e=1` or a development build; `test-hook.ts`).
    const testHook = isTestHookEnabled(window.location.href, import.meta.env.DEV) ? new TestHookRecorder() : undefined;
    const player = new PlayerWebApp({
      elements: {
        stage: document.querySelector<HTMLDivElement>('#stage')!,
        shell: document.querySelector<HTMLDivElement>('.shell')!,
        chartInput: document.querySelector<HTMLInputElement>('#songs')!,
        filesInput: document.querySelector<HTMLInputElement>('#files')!,
        searchInput: document.querySelector<HTMLInputElement>('#search')!,
        loadingOverlay: document.querySelector<HTMLDivElement>('#loading-overlay')!,
        loadingLabel: document.querySelector<HTMLDivElement>('#loading-label')!,
        loadingBarFill: document.querySelector<HTMLElement>('#loading-bar-fill')!,
        loadingCounter: document.querySelector<HTMLDivElement>('#loading-counter')!,
      },
      deviceProfile,
      testHook,
    });
    // Installed before the app starts, so the hook sees the first select screen come up.
    if (testHook !== undefined) {
      const testHookDeps = player.testHookDeps();
      testHook.install(window, () => liveTestHookState(testHookDeps));
    }
    player.start();
  });