UNPKG

function-tree

Version:
108 lines 4.95 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import { isPromise, isPlainValue } from './utils'; var Provider = /*#__PURE__*/function () { function Provider(definition) { var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, _ref$wrap = _ref.wrap, wrap = _ref$wrap === void 0 ? true : _ref$wrap, _ref$ignoreDefinition = _ref.ignoreDefinition, ignoreDefinition = _ref$ignoreDefinition === void 0 ? false : _ref$ignoreDefinition; _classCallCheck(this, Provider); this.definition = definition; if (typeof definition === 'function') { return; } if (!ignoreDefinition) { this.verifyDefinition(definition); } this.wrap = wrap; this.ProviderConstructor = function (context) { this.context = context; }; this.ProviderConstructor.prototype = definition; this.WrappedProviderConstructor = function (name, context) { this.context = context; this.providerName = name; }; this.WrappedProviderConstructor.prototype = Object.keys(ignoreDefinition ? {} : definition).reduce(function (wrappedProvider, key) { var originalFunc = definition[key]; wrappedProvider[key] = function () { var _this = this; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var providerResult = originalFunc.apply(this, args); if (isPromise(providerResult)) { return providerResult.then(function (result) { _this.context["debugger"].send({ type: 'provider', datetime: Date.now(), method: "".concat(_this.providerName, ".").concat(key), args: args, isPromise: true, isRejected: false, returnValue: isPlainValue(result) ? result : '[CAN_NOT_SERIALIZE]' }); return result; })["catch"](function (error) { _this.context["debugger"].send({ method: "".concat(_this.providerName, ".").concat(key), args: args, isPromise: true, isRejected: true }); throw error; }); } this.context["debugger"].send({ type: 'provider', datetime: Date.now(), method: "".concat(this.providerName, ".").concat(key), args: args, returnValue: isPlainValue(providerResult) ? providerResult : '[CAN_NOT_SERIALIZE]' }); return providerResult; }; return wrappedProvider; }, {}); } return _createClass(Provider, [{ key: "verifyDefinition", value: function verifyDefinition(definition) { if (this.ignoreDefinition) { return; } if (_typeof(definition) !== 'object' || definition === null) { throw new Error('The definition passed as Provider is not valid'); } Object.keys(definition).forEach(function (key) { if (typeof definition[key] !== 'function') { throw new Error("The property ".concat(key, " passed to Provider is not a method")); } }); } }, { key: "get", value: function get(context) { if (typeof this.definition === 'function') { return this.definition(context); } return new this.ProviderConstructor(context); } }, { key: "getWrapped", value: function getWrapped(name, context) { if (typeof this.definition === 'function') { return this.definition(context); } return new this.WrappedProviderConstructor(name, context); } }]); }(); export default Provider; //# sourceMappingURL=Provider.js.map