Files

1 line
8.6 KiB
Plaintext
Raw Permalink Normal View History

2025-01-04 00:34:03 +01:00
{"version":3,"file":"TranscoderWorker.mjs","sources":["../src/TranscoderWorker.ts"],"sourcesContent":["import { TranscoderWorkerWrapper } from './TranscoderWorkerWrapper';\n\nimport type { BASIS_FORMATS } from './Basis';\nimport type { ITranscodeResponse } from './TranscoderWorkerWrapper';\n\n/**\n * Worker class for transcoding *.basis files in background threads.\n *\n * To enable asynchronous transcoding, you need to provide the URL to the basis_universal transcoding\n * library.\n * @memberof PIXI.BasisParser\n */\nexport class TranscoderWorker\n{\n // IMPLEMENTATION NOTE: TranscoderWorker tracks transcoding requests with a requestID; the worker can be issued\n // multiple requests (once it is initialized) and the response contains the requestID of the triggering request. Based on\n // the response, the transcodeAsync promise is fulfilled or rejected.\n\n // TODO: Publish our own @pixi/basis package & set default URL to jsdelivr/cdnjs\n /** URL for the script containing the basis_universal library. */\n static bindingURL: string;\n static jsSource: string;\n static wasmSource: ArrayBuffer;\n\n private static _onTranscoderInitializedResolve: () => void;\n\n /** a promise that when reslved means the transcoder is ready to be used */\n public static onTranscoderInitialized = new Promise<void>((resolve) =>\n {\n TranscoderWorker._onTranscoderInitializedResolve = resolve;\n });\n\n isInit: boolean;\n load: number;\n requests: { [id: number]: {\n resolve: (data: ITranscodeResponse) => void,\n reject: () => void\n } } = {};\n\n private static _workerURL: string;\n private static _tempID = 0;\n\n /** Generated URL for the transcoder worker script. */\n static get workerURL(): string\n {\n if (!TranscoderWorker._workerURL)\n {\n let workerSource = TranscoderWorkerWrapper.toString();\n\n const beginIndex = workerSource.indexOf('{');\n const endIndex = workerSource.lastIndexOf('}');\n\n workerSource = workerSource.slice(beginIndex + 1, endIndex);\n\n if (TranscoderWorker.jsSource)\n {\n workerSource = `${TranscoderWorker.jsSource}\\n${workerSource}`;\n }\n\n TranscoderWorker._workerURL = URL.createObjectURL(new Blob([workerSource]));\n }\n\n return TranscoderWorker._workerURL;\n }\n\n protected worker: Worker;\n protected initPromise: Promise<void>;\n protected onInit: () => void;\n\n constructor()\n {\n this.isInit = false;\n this.load = 0;\n this.initPromise = new Promise((resolve) => { this.onInit = resolve; });\n\n if (!TranscoderWorker.wasmSource)\n {\n console.warn('resources.BasisResource.TranscoderWorker has not been given the transcoder WASM binary!');\n }\n\n this.worker = new Worker(TranscoderWorker.workerURL);\n this.worker.onmessage = this.onMessage;\n this.worker.postMessage({\n type: 'init',\n jsSource: TranscoderWorker.jsSource,\n wasmSource: TranscoderWorker.wasmSource\n });\n }\n\n /** @returns a promise that is resolved when the web-worker is initialized */\n initAsync(): Promise<void>\n {\n return this.initPromise;\n }\n\n /**\n * Creates a promise that will resolve when the transcoding of a *.basis file is complete.\n * @param basisData - *.basis file contents\n * @param rgbaFormat - transcoding format for RGBA files\n * @param rgbFormat - transcoding format for RGB files\n * @returns a promise that is resolved with the transcoding response of the web-worker\n */\n async transcodeAsync(\n basisData: Uint8Array,\n rgbaFormat: BASIS_FORMATS,\n rgbFormat: BASIS_FORMATS\n ): Promise<ITranscodeResponse>\n {\n ++this.load;\n\n const requestID = TranscoderWorker._tempID++;\n const requestPromise = new Promise((resolve: (data: ITranscodeResponse) => void, reject: () => voi