@fancy-crud/bus
Version:
Utility package to use command responsibility segregation
117 lines (116 loc) • 2.65 kB
JavaScript
var u = Object.defineProperty;
var f = (r, e, t) => e in r ? u(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
var o = (r, e, t) => (f(r, typeof e != "symbol" ? e + "" : e, t), t);
class x {
constructor() {
o(this, "$meta");
}
}
class y {
}
const l = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map();
class c extends Error {
constructor(e) {
super(`Handler not found for command: ${e}`), Object.setPrototypeOf(this, c.prototype), this.name = "HandlerDoesNotExist";
}
}
class I {
hasImplicitHandler(e) {
return e.meta !== void 0;
}
implicitHandler(e, t) {
let n;
return t && Array.isArray(t) ? n = e.meta.Handler(...t) : n = e.meta.Handler(), n.execute(e);
}
mapHandler(e, t) {
const n = l.get(e.constructor.name);
let s;
return t && Array.isArray(t) ? s = new n(...t) : s = new n(), s.execute(e);
}
execute(e, t) {
return this.hasImplicitHandler(e) ? this.implicitHandler(e, t) : this.mapHandler(e, t);
}
}
class A {
execute(e) {
const t = l.get(e);
if (!t)
throw new c(e);
return t;
}
}
class j {
execute(e) {
let t = [];
return t = e.map((n) => {
const s = a.get(n);
if (!s)
return null;
const i = s.provider, d = this.execute(s.dependencies);
return new i(...d);
}).filter((n) => n !== null), t;
}
}
class h {
execute(e, t) {
let n;
if (typeof e == "string") {
if (!t)
throw new Error("Should specify a provider for naming providers");
n = e, a.set(n, t);
} else
n = e.name, a.set(n, e);
return n;
}
}
const p = new h().execute;
class w {
execute(e, t) {
const n = typeof e == "string" ? e : e.name;
if (!t)
throw new Error("Should specify a handler for naming handlers");
return l.set(n, t), n;
}
}
const m = new w().execute;
function g(r) {
let e;
typeof r == "string" ? e = r : e = r.name;
const t = a.get(e);
if (!t)
throw new Error(`Unable to get provider ${e}`);
return new t();
}
const C = g;
function E(r) {
return {
Handler(...e) {
const t = l.get(r.name);
let n;
if (!t)
throw new c(r.name);
return e && Array.isArray(e) ? n = new t(...e) : n = new t(), n;
}
};
}
function b(r, e) {
p(r, e), m(r, e);
}
export {
I as Bus,
c as HandlerDoesNotExist,
x as IBaseCommand,
y as IBaseHandler,
A as MapCommands,
j as MapDependencies,
w as RegisterCommandHandler,
h as RegisterProvider,
l as handlers,
g as inject,
p as injectable,
C as injecting,
E as meta,
a as providers,
b as register,
m as registerCommandHandler
};