UNPKG

pci-loader

Version:

A scoped PCI Loader for modern web applications.

3 lines (2 loc) 4.48 kB
(function(l,f){typeof exports=="object"&&typeof module<"u"?f(exports,require("systemjs/dist/system.js"),require("systemjs/dist/extras/named-register.js")):typeof define=="function"&&define.amd?define(["exports","systemjs/dist/system.js","systemjs/dist/extras/named-register.js"],f):f((l=typeof globalThis<"u"?globalThis:l||self)["pci-loader"]={})})(this,function(l){"use strict";let f=Promise.resolve();class g{#t;#e;#s;constructor(){this.#t=new System.constructor,this.#e={imports:{},scopes:{}},this.#s=(t,e,o)=>{const[i,s]=(function(u,a,n){const d=typeof u=="string",h=d?u:null,c=d?a:u,m=d?n:a;let p,y;if(Array.isArray(c))p=c,y=m;else if(typeof c=="object")p=[],y=function(){return c};else{if(typeof c!="function")throw Error(w(9,"Invalid call to AMD define()"));p=["require","exports","module"],y=c}return[h,T(p,y)]})(t,e,o);i?(this.#t.registerRegistry[i]=s,this.#t.register(i,s[0],s[1])):this.#t.register(s[0],s[1])},this.#s.amd={}}define(t,e,o=!1){if(typeof e=="string")this.#e.imports[t]=e;else{const i=`app:${t}`;this.#e.imports[t]=i,o||e[Symbol.toStringTag]==="Module"?this.#t.set(i,e):this.#t.set(i,{default:e,__useDefault:!0})}}undefine(t){t in this.#e.imports?(delete this.#e.imports[t],this.#t.delete(`app:${t}`)):this.#t.delete(t)}defined(t){return t in this.#e.imports||this.#t.has(t)}async load(t){return new Promise((e,o)=>{const i=s=>{(s.message?.includes("SystemJS Error")||s.error?.stack?.includes(t))&&(s.preventDefault(),o(s.error))};f=f.then(()=>{try{this.#t.addImportMap(this.#e);const s=globalThis.define;return globalThis.define=this.#s,window.addEventListener("error",i),this.#t.import(t).then(u=>{e(I(u))}).catch(o).finally(()=>{globalThis.define=s,window.removeEventListener("error",i)})}catch(s){return o(s)}})})}}function w(r,t){return`${t} (SystemJS Error#${r} https://github.com/systemjs/systemjs/blob/main/docs/errors.md#${r})`}function v(){throw Error(w(5,"AMD require not supported."))}function I(r){return r?.__useDefault?r?.default:r}function E(r,t){return e=>t[r]=I(e)}function T(r,t){const e=t,o={},i={exports:o},s=[],u=[];let a=0;for(let n=0;n<r.length;n++){const d=r[n],h=u.length;d==="require"?(s[n]=v,a++):d==="module"?(s[n]=i,a++):d==="exports"?(s[n]=o,a++):u.push(E(n,s)),a&&(r[h]=d)}return a&&(r.length-=a),[r,(n,d)=>(n({default:o,__useDefault:!0}),{setters:u,execute(){i.uri=d.meta.url;const h=e.apply(o,s);h!==void 0&&(i.exports=h),n(i.exports),n("default",i.exports)}})]}class x{#t;constructor(){this.#t=new Map}register(t){if(!t?.typeIdentifier||typeof t?.getInstance!="function")throw new TypeError("Invalid interaction");this.#t.set(t.typeIdentifier,t)}getInstance(t,e,o,i){const s=this.#t.get(t);if(!s)throw new ReferenceError(`Interaction "${t}" not found`);s.getInstance(e,o,i)}}class b extends Error{}function P(r,{timeout:t=3e4,message:e="Timeout!"}={}){return Promise.race([r,new Promise((o,i)=>{setTimeout(()=>{i(new b(e))},t)})])}class j{#t;#e;#s;#r;constructor(t,e){this.#t=e,this.#e=t,this.#s="initial",this.#r=Promise.resolve()}get name(){return this.#t}get url(){return this.#e}get status(){return this.#s}async load({timeout:t=3e4}={},e=void 0){return this.#r=this.#r.catch(()=>{e&&e.undefine(this.url)}).then(o=>{if(o)return o;let i=new Promise((s,u)=>{e||(e=new g),e.define("qtiCustomInteractionContext",{register:a=>{try{if(this.#t){if(a.typeIdentifier!==this.#t)throw new TypeError(`Expected PCI '${this.#t}', got '${a.typeIdentifier}' instead`)}else this.#t=a.typeIdentifier;const n=new x;n.register(a),this.#s="loaded",s({getInstance:n.getInstance.bind(n)})}catch(n){u(n)}}}),this.#s="loading",e.load(this.#e).catch(u)});return t&&(i=P(i,{timeout:t,message:"Loading PCI timed out"})),i.catch(s=>{throw this.#s="error",s})}),this.#r}async getInstance(t,e,o,{timeout:i=3e4}={}){const s=Date.now();return this.load({timeout:i}).then(u=>{const a=new Promise((d,h)=>{try{u.getInstance(this.#t,t,{...e,onready:(c,m)=>{e.onready?.(c,m),d([c,m])}},o)}catch(c){h(c)}});if(!i)return a;const n=Date.now()-s;return P(a,{timeout:Math.max(i-n,0),message:"Getting PCI instance timed out"})})}}l.AMDLoader=g,l.PCILoader=class extends j{async load({timeout:r=3e4}={}){return super.load({timeout:r})}},l.PCILoaderDev=class extends j{#t;constructor(r,t){super(r,t),this.#t=new g}async load({timeout:r=3e4}={}){return super.load({timeout:r},this.#t)}define(r,t,e=!1){this.#t.define(r,t,e)}},l.PCIRegistry=x,l.TimeoutError=b,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})}); //# sourceMappingURL=index.umd.js.map