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