All files / player-browser/src/compute index.ts

54.54% Statements 6/11
75% Branches 9/12
57.14% Functions 4/7
54.54% Lines 6/11

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        126x       11x   104x 104x                           54x             115x                    
import { BrowserTaskPool, type TaskWorker } from './pool.ts';
 
/** Half of the browser-reported logical CPU count, rounded down, with at least one worker. */
export function defaultBrowserWorkerCount(cores: number | undefined): number {
  return Number.isFinite(cores) && cores! >= 1 ? Math.max(1, Math.floor(cores! / 2)) : 1;
}
/** Invalid settings return to the hardware-derived default. */
export function normalizeBrowserWorkerCount(value: number, cores: number | undefined): number {
  return Number.isSafeInteger(value) && value >= 1 ? value : defaultBrowserWorkerCount(cores);
}
let count = defaultBrowserWorkerCount(typeof navigator === 'undefined' ? undefined : navigator.hardwareConcurrency);
let pool = new BrowserTaskPool(count);
/** Install a host-bundled worker factory once, before loading files. */
export function configureBrowserWorkers(factory: () => TaskWorker): void {
  pool.dispose();
  pool = new BrowserTaskPool(count, factory);
}
export function setBrowserWorkerCount(value: number): void {
  count = normalizeBrowserWorkerCount(
    value,
    typeof navigator === 'undefined' ? undefined : navigator.hardwareConcurrency,
  );
  pool.resize(count);
}
export function getBrowserWorkerCount(): number {
  return count;
}
export function disposeBrowserWorkers(): void {
  pool.dispose();
}
/** Data-only task messages; the same implementation is used by hosts without workers. */
export function runBrowserTask<T>(name: string, input: unknown, fallback: () => T | Promise<T>): Promise<T> {
  return pool.run(name, input, fallback);
}
export { executeBrowserTask, type ParseChartTaskInput, type ParseChartTaskResult, type ZipTaskInput } from './tasks.ts';
export {
  configureBGAVideoWorkers,
  type BGAVideoWorker,
  type BGAVideoWorkerFactories,
} from '../media/bga-stream/stream.ts';
export { runBGAVideoDecodeWorker } from '../media/bga-stream/decode-worker.ts';
export { runBGAVideoEncodeWorker } from '../media/bga-stream/encode-worker.ts';