@asimojs/asimo
Version:
Asynchronous dependency manager for Typescript projects
229 lines (228 loc) • 6.9 kB
JavaScript
var M = function(f, b, u, y) {
function C(l) {
return l instanceof u ? l : new u(function(a) {
a(l);
});
}
return new (u || (u = Promise))(function(l, a) {
function S(h) {
try {
v(y.next(h));
} catch (_) {
a(_);
}
}
function g(h) {
try {
v(y.throw(h));
} catch (_) {
a(_);
}
}
function v(h) {
h.done ? l(h.value) : C(h.value).then(S, g);
}
v((y = y.apply(f, b || [])).next());
});
};
function J(f) {
return { ns: f };
}
const E = "service:", F = "object:", N = "group:", U = Promise.resolve(null), L = "color: #669df6", R = "color: #e39f00;font-weight:bold";
let $ = console, P = 0;
const m = Symbol("NOT_FOUND"), k = Promise.resolve(m);
function G(f) {
let b = "", u = null;
typeof f == "string" ? b = f || `AsmContext${++P}` : (b = (f == null ? void 0 : f.name) || `AsmContext${++P}`, u = (f == null ? void 0 : f.parent) || null), b = b.replace(/\//g, "\\/");
const y = `${(u == null ? void 0 : u.path) || ""}/${b}`;
let C = 0;
const l = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map();
let S = null;
const g = {
/** The context name */
get name() {
return b;
},
/** The parent context, if any */
get parent() {
return u;
},
/** The interface path, built from the context parent path */
get path() {
return y;
},
/** The interface definitions that are currently loaded in the context */
get definitions() {
var t;
const e = [];
for (const r of l.keys()) {
const n = r.split(":"), o = {
iid: (t = n[1]) !== null && t !== void 0 ? t : "",
type: n[0]
};
o.type === "service" && (o.loaded = a.has(r)), e.push(o);
}
return e;
},
/** Register an object instance */
registerObject(t, e) {
w(t, null, "registerObject") && (S || (S = /* @__PURE__ */ new Map()), S.set(t.ns, e));
},
/** Register a service factory */
registerService(t, e) {
w(t, e, "registerService") && (l.set(E + t.ns, e), A(t.ns));
},
/** Register an object factory */
registerFactory(t, e) {
w(t, e, "registerFactory") && (l.set(F + t.ns, e), A(t.ns));
},
/** Register a factory group */
registerGroup(t, e) {
const r = N + ++C;
if (typeof e != "function") {
p(`[registerGroup] Invalid group loader: ${I(e)}`);
return;
}
const n = () => {
let o = a.get(r);
return o || (o = x(e, g), a.set(r, o), o);
};
for (const o of t)
w(o, null, "registerGroup") && l.set(N + o.ns, n);
},
/** Get a registered object */
get(t, e, ...r) {
const n = v(t);
if (e == null || typeof e != "object" || !("ns" in e) || typeof e.ns != "string") {
const o = h(n);
if (o === m) {
if (e !== void 0)
return e;
p(`Object not found: ${I(n)}`, !0);
}
return o;
} else {
const i = [n, v(e), ...r.map((s) => v(s))], d = i.map((s) => h(s)), c = [];
for (let s = 0; d.length > s; s++)
d[s] === m && c.push(i[s]);
return c.length && (c.length === 1 ? p(`Object not found: "${c[0]}"`, !0) : p(`Objects not found: ["${c.join('", "')}"]`, !0)), d;
}
},
/** Fetch a service or an object instance (services have priority) */
fetch(t, e, ...r) {
return M(this, void 0, void 0, function* () {
const n = v(t);
if (e == null || typeof e != "object" || !("ns" in e) || typeof e.ns != "string") {
const o = yield _(n, !0);
if (o === m) {
if (e !== void 0)
return e;
p(`Interface not found: ${I(n)}`, !0);
}
return o;
} else {
const i = [n, v(e), ...r.map((s) => v(s))], d = yield Promise.all(i.map((s) => _(s, !0))), c = [];
for (let s = 0; d.length > s; s++)
d[s] === m && c.push(i[s]);
return c.length && (c.length === 1 ? p(`Interface not found: "${c[0]}"`, !0) : p(`Interfaces not found: ["${c.join('", "')}"]`, !0)), d;
}
});
},
createChildContext(t) {
return G({ name: t, parent: g });
},
get logger() {
return $;
},
set logger(t) {
$ = t;
},
/**
* Log the asimo state into an array or in the console if no output argument is provided
* @param output
*/
logState(t) {
var e;
const r = t || [], n = g.definitions;
r.push(`Context ${g.path}${n.length === 0 ? " [empty]" : ":"}`), r.push(...n.map((i) => `+ ${i.iid} [${i.type}]${o(i)}`)), (e = g.parent) === null || e === void 0 || e.logState(r), t || $ == null || $.log(r);
function o(i) {
return i.type === "service" ? i.loaded ? ": loaded" : ": not loaded" : "";
}
}
};
return g;
function v(t) {
return typeof t == "string" ? t : t.ns;
}
function h(t) {
const e = S == null ? void 0 : S.get(t);
return e !== void 0 ? e : u ? u.get(t, m) : m;
}
function _(t) {
return M(this, arguments, void 0, function* (e, r = !0) {
const n = E + e, o = a.get(n);
if (o !== void 0)
return o;
const i = l.get(n);
if (i) {
let d = x(i, g), c;
const s = new Promise((j) => {
c = j;
});
return d.then((j) => {
j && (typeof j == "object" || typeof j == "function") ? (a.set(n, j), c(j)) : (p(`Invalid factory output: "${e}"`), a.set(n, U), c(m));
}), a.set(n, s), s;
} else {
const d = l.get(F + e);
if (d)
return x(d, g);
if (r) {
const c = l.get(N + e);
if (c)
return x(c, g).then(() => _(e, !1));
}
if (u)
return u.fetch(e, m);
}
return k;
});
}
function w(t, e, r) {
return typeof t != "object" || typeof t.ns != "string" || t.ns === "" ? (p(`[${r}] Invalid interface id: ${I(t)}`), !1) : e && typeof e != "function" ? (p(`[${r}] Invalid factory: ${I(e)}`), !1) : !0;
}
function I(t) {
let e = "" + t;
try {
e = JSON.stringify(t);
} catch {
}
return e;
}
function A(t) {
l.delete(N + t);
}
function p(t, e = !1) {
if ($ === console ? console.log(`%cASM [${y}] %c${t}`, L, "color: ", R) : $ == null || $.log(`ASM [${y}] ${t}`), e)
throw new Error(`ASM [${y}] ${t}`);
}
function x(t, e) {
return M(this, void 0, void 0, function* () {
let r = null;
try {
r = yield t(e);
} catch (n) {
r = null, p(`Instantiation error: ${(n == null ? void 0 : n.message) || n}`);
}
return r;
});
}
}
let T;
globalThis.asm ? T = globalThis.asm : T = G("asm");
globalThis.asm = T;
const B = T;
export {
B as asm,
G as createContext,
J as interfaceId
};