UNPKG

@difizen/mana-core

Version:

80 lines 5.54 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); } var _dec, _class; function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(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 { contrib, inject, singleton } from '@difizen/mana-syringe'; import { ManaContext } from "../module"; import { DataContextContriburtion, DataContextSymbol } from "./data-context-protocol"; export var DataContextManager = (_dec = singleton(), _dec(_class = /*#__PURE__*/function () { function DataContextManager(provider, context) { _classCallCheck(this, DataContextManager); this.contruibutions = []; this.contextMap = new Map(); this.provider = provider; this.context = context; } DataContextManager = inject(DataContextSymbol)(DataContextManager, undefined, 1) || DataContextManager; DataContextManager = contrib(DataContextContriburtion)(DataContextManager, undefined, 0) || DataContextManager; _createClass(DataContextManager, [{ key: "getContributionMap", value: function getContributionMap() { if (this.contributionMap) { return this.contributionMap; } this.contributionMap = new Map(); var _iterator = _createForOfIteratorHelper(this.provider.getContributions()), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var contribution = _step.value; this.contributionMap.set(contribution.key, contribution); this.contruibutions.push(contribution); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return this.contributionMap; } }, { key: "create", value: function create(contributionKey, options) { var contribution = this.getContributionMap().get(contributionKey); if (contribution) { var child = this.context.container.createChild(); var ctx = new ManaContext(child); child.register({ token: DataContextSymbol, useDynamic: function useDynamic() { return ctx; } }); child.load(contribution.module); if (contribution.onCreate) { contribution.onCreate(ctx, options); } var key; if (contribution.getKey) { key = contribution.getKey(ctx, options); } else { key = ctx; } this.contextMap.set(key, ctx); return key; } } }, { key: "getContext", value: function getContext(key) { return this.contextMap.get(key); } }]); return DataContextManager; }()) || _class);