corello
Version:
Main Kernel
189 lines (188 loc) • 6.16 kB
JavaScript
var E = Object.defineProperty;
var d = (e, t, s) => t in e ? E(e, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : e[t] = s;
var h = (e, t, s) => d(e, typeof t != "symbol" ? t + "" : t, s);
function m(e, t) {
let s = e;
for (; s; ) {
if (s === t)
return !0;
s = Object.getPrototypeOf(s);
}
return !1;
}
const x = (e) => typeof e == "function", D = (e) => Object.getOwnPropertyNames(e).map((t) => ({ prop: t, descriptor: Object.getOwnPropertyDescriptor(e, t) })), P = ({ instance: e, methodPredicate: t, withObjProps: s }) => {
const r = {}, i = Object.getOwnPropertyNames(Object.getPrototypeOf({})), c = (l) => {
D(l).forEach((n) => {
!s && i.includes(n.prop) || r[n.prop] !== void 0 || (t && x(l[n.prop]) ? t(n.prop) && (r[n.prop] = n) : r[n.prop] = n);
}), l !== Object.prototype && c(Object.getPrototypeOf(l));
};
return c(e), Object.freeze(r);
}, g = (e) => e instanceof Date, b = (e) => typeof e == "function" && /^class\s/.test(Function.prototype.toString.call(e)), v = (e) => e !== Object(e), j = Symbol("?"), $ = () => {
if ("crypto" in window && x(crypto.randomUUID)) return crypto.randomUUID();
{
let e = (/* @__PURE__ */ new Date()).getTime();
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(t) {
const s = (e + Math.random() * 16) % 16 | 0;
return e = Math.floor(e / 16), (t === "x" ? s : s & 3 | 8).toString(16);
});
}
}, a = Symbol(), O = Symbol();
function A(e) {
if (!m(e, _)) throw new Error(`${e.name} needs to have DtoBase in its inhertiance chain.`);
const t = e.name || "";
return class extends e {
constructor(...s) {
super(s == null ? void 0 : s[0]);
const r = (s == null ? void 0 : s[0]) || {};
!this[j] && Object.defineProperty(this, j, {
enumerable: !0,
configurable: !1,
get: function() {
return Object.freeze({ name: e.name, instance: this });
}
}), !this[O] && Object.defineProperty(this, O, {
enumerable: !1,
configurable: !1,
value: function(i) {
const c = P({ instance: this }), l = (n, u, o, y, ...p) => {
if (Array.isArray(u)) {
if (!Array.isArray(o)) throw new Error(`Expected Array for [${n}] but the received json is not an Array!`);
return o.map((f, w) => l(n, f, f, y, w));
} else if (g(u))
if (y) {
const f = y();
return b(f) || f === Date ? new f(o, ...p) : f(o, ...p);
} else
return o ? new Date(o) : /* @__PURE__ */ new Date();
else if (y) {
const f = y();
return b(f) || f === Date ? new f(o, ...p) : f(o, ...p);
} else
return v(u) ? o : (Object.getPrototypeOf(u) === Object.prototype && console.warn(`Missing a possible factory to serialize ${t ? t + "." : ""}${n}, will treat [${JSON.stringify(u)}] as plain object`), u.constructor ? new u.constructor(o) : o);
};
Object.entries(c).filter(([n, u]) => {
var o;
return !((o = u.descriptor) != null && o.get);
}).forEach(([n, u]) => {
var o;
i[n] !== void 0 && (this[n] = l(n, this[n], i[n], (o = this[a]) == null ? void 0 : o[n]));
});
}
}), this[O](r);
}
toJSON() {
const s = Object.assign({}, this), r = (i) => {
Object.entries(Object.getOwnPropertyDescriptors(i)).filter(([c, l]) => x(l.get)).map(([c, l]) => {
if (l && c[0] !== "_")
try {
const n = this[c];
s[c] = n;
} catch (n) {
console.error(`Error calling getter ${c}`, n);
}
}), i !== Object.prototype && r(Object.getPrototypeOf(i));
};
return r(this), s;
}
toString() {
return `[DTO:${t}]`;
}
};
}
function N(e) {
return function(t, s) {
!t[a] && (t[a] = {}), t[a][s] = e;
};
}
function F(e) {
return function(t, s) {
if (b(e())) throw new Error(`Factory must return an instance, update your factory for property ${s} to return NEW instnace`);
!t[a] && (t[a] = {}), t[a][s] = () => e;
};
}
class _ {
constructor(t) {
}
from(t) {
return this[O](t);
}
}
class U {
constructor(t, s, r = "Event") {
h(this, "type");
h(this, "info");
this.type = `${r}@${t}`, this.info = s;
}
}
class z {
constructor() {
h(this, "id", $());
h(this, "listeners", {});
h(this, "target", null);
h(this, "_isOn", !0);
}
once(t, s) {
this.validateEvent(t), this.on(t, s, { once: !0 });
}
get isOn() {
return this._isOn;
}
turnOn() {
this._isOn = !0;
}
turnOff() {
this._isOn = !1;
}
on(t, s, r = {}) {
this.validateEvent(t);
const { type: i } = t;
i in this.listeners || (this.listeners[i] = []), this.listeners[i].push({ callback: s, options: r });
}
off(t, s) {
this.validateEvent(t);
const { type: r } = t;
if (!(r in this.listeners))
return;
const i = this.listeners[r];
if (s) {
for (let c = 0, l = i.length; c < l; c++)
if (i[c].callback === s) {
i.splice(c, 1);
return;
}
} else i.length = 0;
}
dispatch(t) {
if (this.validateEvent(t), !!this._isOn) {
if (!(t.type in this.listeners))
return !0;
this.listeners[t.type].forEach((s, r) => {
s.callback.call(this, t), s.options.once && this.listeners[t.type].splice(r, 1, void 0);
}), this.listeners[t.type] = this.listeners[t.type].filter((s) => !!s);
}
}
dispose() {
Object.keys(this.listeners).forEach((t) => {
this.listeners[t].length = 0, delete this.listeners[t];
});
}
}
const B = (e, t) => {
if (!(e instanceof t) && !m(t, e.constructor)) throw new Error("To dispatch events use the class Event");
};
export {
N as Class,
z as DispatcherBase,
A as Dto,
_ as DtoBase,
U as Event,
F as Factory,
P as extract,
m as hasParentClass,
b as isClass,
g as isDate,
x as isFn,
v as isPrimitive,
$ as uid,
B as validate
};