UNPKG

@wener/console

Version:
428 lines (427 loc) 16.9 kB
function _array_like_to_array(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 _array_without_holes(arr) { if (Array.isArray(arr)) return _array_like_to_array(arr); } function _assert_this_initialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _async_to_generator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _call_super(_this, derived, args) { derived = _get_prototype_of(derived); return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args)); } function _class_call_check(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, descriptor.key, descriptor); } } function _create_class(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _define_property(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _get_prototype_of(o) { _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _get_prototype_of(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _set_prototype_of(subClass, superClass); } function _iterable_to_array(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _non_iterable_spread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _possible_constructor_return(self, call) { if (call && (_type_of(call) === "object" || typeof call === "function")) { return call; } return _assert_this_initialized(self); } function _set_prototype_of(o, p) { _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _set_prototype_of(o, p); } function _to_consumable_array(arr) { return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread(); } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function _unsupported_iterable_to_array(o, minLen) { if (!o) return; if (typeof o === "string") return _array_like_to_array(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(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); } function _is_native_reflect_construct() { try { var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () { })); } catch (_) { } return (_is_native_reflect_construct = function () { return !!result; })(); } function _ts_generator(thisArg, body) { var f, y, t, _ = { label: 0, sent: function () { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty; return d(g, "next", { value: verb(0) }), d(g, "throw", { value: verb(1) }), d(g, "return", { value: verb(2) }), typeof Symbol === "function" && d(g, Symbol.iterator, { value: function () { return this; } }), g; function verb(n) { return function (v) { return step([ n, v ]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [ op[0] & 2, t.value ]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [ 0 ]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [ 6, e ]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } import { createStore } from "zustand"; import { immer } from "zustand/middleware/immer"; import { DynamicStore } from "./DynamicStore.js"; var DynamicModuleContext = /*#__PURE__*/ function (DynamicStore) { "use strict"; _inherits(DynamicModuleContext, DynamicStore); function DynamicModuleContext(module, store) { _class_call_check(this, DynamicModuleContext); var _this; _this = _call_super(this, DynamicModuleContext, [ store ]), _define_property(_this, "module", void 0), _this.module = module; return _this; } return DynamicModuleContext; }(DynamicStore); export var ModuleService = /*#__PURE__*/ function () { "use strict"; function ModuleService(loader) { _class_call_check(this, ModuleService); _define_property(this, "loader", void 0); _define_property(this, "store", void 0); _define_property(this, "instances", void 0); this.loader = loader; this.store = createStore(immer(function () { return {}; })); this.instances = []; } _create_class(ModuleService, [ { key: "modules", get: function get() { return this.instances.map(function (v) { return v.module; }); } }, { key: "loadModules", value: function loadModules(names) { return _async_to_generator(function () { var _this, last, loaded; return _ts_generator(this, function (_state) { switch (_state.label) { case 0: _this = this; last = this.instances.map(function (v) { return v.name; }); names = names.filter(function (v) { return last.indexOf(v) < 0; }); return [ 4, Promise.all(names.map(function (name) { return _async_to_generator(function () { var module; return _ts_generator(this, function (_state) { switch (_state.label) { case 0: return [ 4, this.loader(name) ]; case 1: module = _state.sent(); return [ 2, { module: module, name: name, context: new DynamicModuleContext(module, this.store) } ]; } }); }).call(_this); })) ]; case 1: loaded = _state.sent(); // ensure order return [ 4, Promise.all(loaded.map(function (param) { var module = param.module, context = param.context; return _async_to_generator(function () { var _module_onModuleInit; return _ts_generator(this, function (_state) { switch (_state.label) { case 0: if (!("onModuleInit" in module)) return [ 3, 2 ]; return [ 4, (_module_onModuleInit = module.onModuleInit) === null || _module_onModuleInit === void 0 ? void 0 : _module_onModuleInit.call(module, context) ]; case 1: _state.sent(); _state.label = 2; case 2: return [ 2 ]; } }); })(); })) ]; case 2: _state.sent(); this.instances = _to_consumable_array(this.instances).concat(_to_consumable_array(loaded)); // wait to next tick, ensure the SetState take effects return [ 4, Promise.resolve() ]; case 3: _state.sent(); return [ 2 ]; } }); }).call(this); } }, { key: "createRoutes", value: function createRoutes() { return _async_to_generator(function () { var all; return _ts_generator(this, function (_state) { switch (_state.label) { case 0: return [ 4, Promise.all(this.instances.map(function (v) { var _v_module_createRoutes, _v_module; return (_v_module = v.module) === null || _v_module === void 0 ? void 0 : (_v_module_createRoutes = _v_module.createRoutes) === null || _v_module_createRoutes === void 0 ? void 0 : _v_module_createRoutes.call(_v_module, v.context); })) ]; case 1: all = _state.sent(); return [ 2, all.flat().filter(function (x) { return Boolean(x); }) ]; } }); }).call(this); } } ]); return ModuleService; }();