UNPKG

honion

Version:
112 lines (111 loc) 4.46 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _Context_instances, _Context_singletonBag_get, _Context_scopedBag, _Context_bag, _Context_getBagValue; Object.defineProperty(exports, "__esModule", { value: true }); exports.Context = void 0; const isPlainObject = (fn) => { if (typeof fn != "object") { return false; } const proto = Object.getPrototypeOf(fn); if (proto === null) { return true; } const ctor = Object.prototype.hasOwnProperty.call(proto, "constructor") && proto.constructor; return (typeof ctor == "function" && ctor instanceof ctor && Function.prototype.toString.call(ctor) == Function.prototype.toString.call(Object)); }; class Context { constructor() { _Context_instances.add(this); _Context_scopedBag.set(this, {}); _Context_bag.set(this, {}); this.errorStack = []; } get logger() { return this.honion.logger; } set logger(val) { this.honion.logger = val; } get(key) { if (key in __classPrivateFieldGet(this, _Context_instances, "a", _Context_singletonBag_get)) { return __classPrivateFieldGet(this, _Context_instances, "m", _Context_getBagValue).call(this, key, __classPrivateFieldGet(this, _Context_instances, "a", _Context_singletonBag_get)[key]); } else if (key in __classPrivateFieldGet(this, _Context_scopedBag, "f")) { return __classPrivateFieldGet(this, _Context_instances, "m", _Context_getBagValue).call(this, key, __classPrivateFieldGet(this, _Context_scopedBag, "f")[key]); } else { const result = __classPrivateFieldGet(this, _Context_bag, "f")[key]; return __classPrivateFieldGet(this, _Context_instances, "m", _Context_getBagValue).call(this, key, result); } } set(key, arg1, arg2) { if (typeof arg2 != "undefined") { __classPrivateFieldGet(this, _Context_bag, "f")[key] = { type: arg1, builder: arg2, isBuilderBag: true, }; return this; } else { __classPrivateFieldGet(this, _Context_bag, "f")[key] = arg1; return this; } } has(key) { return key in __classPrivateFieldGet(this, _Context_bag, "f"); } delete(key) { const hasKey = this.has(key); delete __classPrivateFieldGet(this, _Context_bag, "f")[key]; delete __classPrivateFieldGet(this, _Context_instances, "a", _Context_singletonBag_get)[key]; delete __classPrivateFieldGet(this, _Context_scopedBag, "f")[key]; return hasKey; } get length() { return Object.keys(__classPrivateFieldGet(this, _Context_bag, "f")).length; } catchError(err) { this.errorStack.push(err); return this; } } exports.Context = Context; _Context_scopedBag = new WeakMap(), _Context_bag = new WeakMap(), _Context_instances = new WeakSet(), _Context_singletonBag_get = function _Context_singletonBag_get() { var _a; const key = "@hal-wang/honion/singletonBag"; const singletonBag = (_a = this.honion[key]) !== null && _a !== void 0 ? _a : {}; this.honion[key] = singletonBag; return singletonBag; }, _Context_getBagValue = function _Context_getBagValue(key, result) { if (isPlainObject(result) && result.isBuilderBag) { if (result.type == "transient") { return result.builder(); } else { let dict; if (result.type == "singleton") { dict = __classPrivateFieldGet(this, _Context_instances, "a", _Context_singletonBag_get); } else { dict = __classPrivateFieldGet(this, _Context_scopedBag, "f"); } if (!(key in dict)) { dict[key] = result.builder(); } return dict[key]; } } else { return result; } };