UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 1.65 kB
class e{static loadScript(e,l){const t=document.createElement("script");t.setAttribute("src",e),t.onload=()=>{l(null)},t.onerror=()=>{l(`Failed to load script='${e}'`)},document.body.appendChild(t)}static loadWasm(l,t,n){const i=e.wasmSupported()&&t.glueUrl&&t.wasmUrl?t.glueUrl:t.fallbackUrl;i?e.loadScript(i,(e=>{if(e)n(e,null);else{const e=window[l];window[l]=void 0,e({locateFile:()=>t.wasmUrl,onAbort:()=>{n("wasm module aborted.")}}).then((e=>{n(null,e)}))}})):n("No supported wasm modules found.",null)}static getModule(l){return e.modules.hasOwnProperty(l)||(e.modules[l]={config:null,initializing:!1,instance:null,callbacks:[]}),e.modules[l]}static initialize(l,t){if(t.initializing)return;const n=t.config;(n.glueUrl||n.wasmUrl||n.fallbackUrl)&&(t.initializing=!0,e.loadWasm(l,n,((e,i)=>{e?n.errorHandler?n.errorHandler(e):console.error(`failed to initialize module=${l} error=${e}`):(t.instance=i,t.callbacks.forEach((e=>{e(i)})))})))}}e.modules={},e.wasmSupported=(e=>{const l={};let t=l;return()=>(t===l&&(t=e()),t)})((()=>{try{if("object"==typeof WebAssembly&&"function"==typeof WebAssembly.instantiate){const e=new WebAssembly.Module(Uint8Array.of(0,97,115,109,1,0,0,0));if(e instanceof WebAssembly.Module)return new WebAssembly.Instance(e)instanceof WebAssembly.Instance}}catch(e){}return!1}));class l{static setConfig(l,t){const n=e.getModule(l);n.config=t,n.callbacks.length>0&&e.initialize(l,n)}static getConfig(l){var t;return null==(t=e.modules)||null==(t=t[l])?void 0:t.config}static getInstance(l,t){const n=e.getModule(l);n.instance?t(n.instance):(n.callbacks.push(t),n.config&&e.initialize(l,n))}}export{l as WasmModule};