UNPKG

@pisell/pisellos

Version:

一个可扩展的前端模块化SDK框架,支持插件系统

595 lines (593 loc) 38.3 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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } 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 _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; } 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 _asyncToGenerator(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 _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 _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); } function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; } 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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } 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 { BaseModule } from "../../../modules/BaseModule"; import { FloorPlanHooks } from "./types"; var FLOOR_PLAN_SYNC_DEBOUNCE_MS = 3000; /** * 平面图模块:预加载列表、pubsub 同步、供 OS Server 路由读本地 store */ export var FloorPlanModule = /*#__PURE__*/function (_BaseModule) { _inherits(FloorPlanModule, _BaseModule); var _super = _createSuper(FloorPlanModule); function FloorPlanModule(name, version) { var _this; _classCallCheck(this, FloorPlanModule); _this = _super.call(this, name, version); _defineProperty(_assertThisInitialized(_this), "defaultName", 'floor_plan'); _defineProperty(_assertThisInitialized(_this), "defaultVersion", '1.0.0'); _defineProperty(_assertThisInitialized(_this), "store", void 0); _defineProperty(_assertThisInitialized(_this), "request", void 0); _defineProperty(_assertThisInitialized(_this), "logger", void 0); _defineProperty(_assertThisInitialized(_this), "floorPlanDataSource", void 0); _defineProperty(_assertThisInitialized(_this), "pendingSyncMessages", []); _defineProperty(_assertThisInitialized(_this), "syncTimer", void 0); return _this; } _createClass(FloorPlanModule, [{ key: "initialize", value: function () { var _initialize = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(core, options) { var _options$initialState; var appPlugin, app; return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: this.core = core; this.store = options.store; if (Array.isArray(options === null || options === void 0 || (_options$initialState = options.initialState) === null || _options$initialState === void 0 ? void 0 : _options$initialState.list) && options.initialState.list.length > 0) { this.store.list = options.initialState.list; this.syncMaps(); this.core.effects.emit(FloorPlanHooks.onFloorPlansChanged, this.store.list); } else { this.store.list = []; this.store.map = new Map(); this.store.codeMap = new Map(); } this.request = core.getPlugin('request'); appPlugin = core.getPlugin('app'); if (appPlugin) { app = appPlugin.getApp(); this.logger = app.logger; } this.initFloorPlanDataSource(); this.setupFloorPlanSync(); case 8: case "end": return _context.stop(); } }, _callee, this); })); function initialize(_x, _x2) { return _initialize.apply(this, arguments); } return initialize; }() }, { key: "logInfo", value: function logInfo(title, metadata) { try { var _this$logger, _this$logger$addLog; (_this$logger = this.logger) === null || _this$logger === void 0 || (_this$logger$addLog = _this$logger.addLog) === null || _this$logger$addLog === void 0 || _this$logger$addLog.call(_this$logger, { type: 'info', title: "[FloorPlanModule] ".concat(title), metadata: metadata || {} }); } catch (_unused) { // ignore } } }, { key: "logWarning", value: function logWarning(title, metadata) { try { var _this$logger2, _this$logger2$addLog; (_this$logger2 = this.logger) === null || _this$logger2 === void 0 || (_this$logger2$addLog = _this$logger2.addLog) === null || _this$logger2$addLog === void 0 || _this$logger2$addLog.call(_this$logger2, { type: 'warning', title: "[FloorPlanModule] ".concat(title), metadata: metadata || {} }); } catch (_unused2) { // ignore } } }, { key: "logError", value: function logError(title, metadata) { try { var _this$logger3, _this$logger3$addLog; (_this$logger3 = this.logger) === null || _this$logger3 === void 0 || (_this$logger3$addLog = _this$logger3.addLog) === null || _this$logger3$addLog === void 0 || _this$logger3$addLog.call(_this$logger3, { type: 'error', title: "[FloorPlanModule] ".concat(title), metadata: metadata || {} }); } catch (_unused3) { // ignore } } }, { key: "idKey", value: function idKey(id) { return String(id); } }, { key: "syncMaps", value: function syncMaps() { this.store.map = new Map(); this.store.codeMap = new Map(); var _iterator = _createForOfIteratorHelper(this.store.list), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var item = _step.value; if ((item === null || item === void 0 ? void 0 : item.id) == null) continue; var key = this.idKey(item.id); this.store.map.set(key, item); if (item.code) { this.store.codeMap.set(String(item.code), item); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } /** 与接口约定一致:sort 升序,id 降序 */ }, { key: "getSortedList", value: function getSortedList() { return _toConsumableArray(this.store.list).sort(function (a, b) { var _a$sort, _b$sort; var sortDiff = Number((_a$sort = a.sort) !== null && _a$sort !== void 0 ? _a$sort : 0) - Number((_b$sort = b.sort) !== null && _b$sort !== void 0 ? _b$sort : 0); if (sortDiff !== 0) return sortDiff; return Number(b.id) - Number(a.id); }); } }, { key: "getById", value: function getById(id) { return this.store.map.get(this.idKey(id)); } }, { key: "getByCode", value: function getByCode(code) { return this.store.codeMap.get(String(code)); } }, { key: "replaceStore", value: function replaceStore(list) { this.store.list = list.map(function (i) { return _objectSpread({}, i); }); this.syncMaps(); } }, { key: "mergeItems", value: function mergeItems(items) { if (items.length === 0) return; var byId = new Map(this.store.map); var _iterator2 = _createForOfIteratorHelper(items), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var item = _step2.value; if ((item === null || item === void 0 ? void 0 : item.id) == null) continue; byId.set(this.idKey(item.id), _objectSpread({}, item)); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } this.store.list = Array.from(byId.values()); this.syncMaps(); } }, { key: "removeByIds", value: function removeByIds(ids) { var _this2 = this; if (ids.length === 0) return; var set = new Set(ids.map(function (id) { return _this2.idKey(id); })); this.store.list = this.store.list.filter(function (p) { return !set.has(_this2.idKey(p.id)); }); this.syncMaps(); } }, { key: "loadListFromServer", value: function () { var _loadListFromServer = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() { var res, body, raw, list; return _regeneratorRuntime().wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.prev = 0; _context2.next = 3; return this.request.get("/schedule/floor-plan", {}, { isShopApi: true }); case 3: res = _context2.sent; body = res; raw = (body === null || body === void 0 ? void 0 : body.data) !== undefined ? body.data : body; list = Array.isArray(raw) ? raw : []; return _context2.abrupt("return", list); case 10: _context2.prev = 10; _context2.t0 = _context2["catch"](0); this.logWarning('loadListFromServer 失败', { error: _context2.t0 instanceof Error ? _context2.t0.message : String(_context2.t0) }); return _context2.abrupt("return", []); case 14: case "end": return _context2.stop(); } }, _callee2, this, [[0, 10]]); })); function loadListFromServer() { return _loadListFromServer.apply(this, arguments); } return loadListFromServer; }() }, { key: "fetchByIdFromServer", value: function () { var _fetchByIdFromServer = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(id) { var res, body, data; return _regeneratorRuntime().wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _context3.prev = 0; _context3.next = 3; return this.request.get("/schedule/floor-plan/".concat(id), {}, { isShopApi: true }); case 3: res = _context3.sent; body = res; data = (body === null || body === void 0 ? void 0 : body.data) !== undefined ? body.data : body; return _context3.abrupt("return", data && _typeof(data) === 'object' ? data : null); case 9: _context3.prev = 9; _context3.t0 = _context3["catch"](0); return _context3.abrupt("return", null); case 12: case "end": return _context3.stop(); } }, _callee3, this, [[0, 9]]); })); function fetchByIdFromServer(_x3) { return _fetchByIdFromServer.apply(this, arguments); } return fetchByIdFromServer; }() }, { key: "fetchByCodeFromServer", value: function () { var _fetchByCodeFromServer = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(code) { var enc, res, body, data; return _regeneratorRuntime().wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: _context4.prev = 0; enc = encodeURIComponent(code); _context4.next = 4; return this.request.get("/schedule/floor-plan/code/".concat(enc), {}, { isShopApi: true }); case 4: res = _context4.sent; body = res; data = (body === null || body === void 0 ? void 0 : body.data) !== undefined ? body.data : body; return _context4.abrupt("return", data && _typeof(data) === 'object' ? data : null); case 10: _context4.prev = 10; _context4.t0 = _context4["catch"](0); return _context4.abrupt("return", null); case 13: case "end": return _context4.stop(); } }, _callee4, this, [[0, 10]]); })); function fetchByCodeFromServer(_x4) { return _fetchByCodeFromServer.apply(this, arguments); } return fetchByCodeFromServer; }() }, { key: "preload", value: function () { var _preload = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() { var list; return _regeneratorRuntime().wrap(function _callee5$(_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: this.logInfo('开始预加载平面图列表'); _context5.next = 3; return this.loadListFromServer(); case 3: list = _context5.sent; if (list.length > 0) { this.replaceStore(list); this.core.effects.emit(FloorPlanHooks.onFloorPlansChanged, this.getSortedList()); this.logInfo('预加载完成', { count: list.length }); } else { this.logWarning('预加载完成但列表为空'); } case 5: case "end": return _context5.stop(); } }, _callee5, this); })); function preload() { return _preload.apply(this, arguments); } return preload; }() }, { key: "initFloorPlanDataSource", value: function initFloorPlanDataSource() { var _serverOptions; var Ctor = (_serverOptions = this.core.serverOptions) === null || _serverOptions === void 0 || (_serverOptions = _serverOptions.All_DATA_SOURCES) === null || _serverOptions === void 0 ? void 0 : _serverOptions.FloorPlanDataSource; if (!Ctor) { this.logWarning('FloorPlanDataSource 不可用,跳过 pubsub 同步'); return; } this.floorPlanDataSource = new Ctor(); this.logInfo('FloorPlanDataSource 实例已创建'); } }, { key: "setupFloorPlanSync", value: function setupFloorPlanSync() { var _this3 = this; if (!this.floorPlanDataSource) return; var flush = function flush() { if (_this3.syncTimer) clearTimeout(_this3.syncTimer); _this3.syncTimer = setTimeout(function () { _this3.processFloorPlanSyncMessages(); }, FLOOR_PLAN_SYNC_DEBOUNCE_MS); }; this.floorPlanDataSource.run({ pubsub: { callback: function callback(res) { var data = (res === null || res === void 0 ? void 0 : res.data) || res; if (!data || data.module !== 'floor_plan') return; _this3.pendingSyncMessages.push(_objectSpread({}, data)); flush(); } } }).catch(function (err) { _this3.logError('setupFloorPlanSync: DataSource run 出错', { error: err instanceof Error ? err.message : String(err) }); }); this.logInfo('floor_plan pubsub 订阅已建立'); } }, { key: "processFloorPlanSyncMessages", value: function () { var _processFloorPlanSyncMessages = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() { var messages, changed, deleteIds, bodyItems, needFullList, partialIds, _iterator3, _step3, _msg$ids2, msg, _msg$ids, body, hasBody, uniqueDeletes, list, fetched, _iterator4, _step4, id, one; return _regeneratorRuntime().wrap(function _callee6$(_context6) { while (1) switch (_context6.prev = _context6.next) { case 0: messages = _toConsumableArray(this.pendingSyncMessages); this.pendingSyncMessages = []; if (!(messages.length === 0)) { _context6.next = 4; break; } return _context6.abrupt("return"); case 4: this.logInfo('processFloorPlanSyncMessages 开始', { count: messages.length }); changed = false; deleteIds = []; bodyItems = []; needFullList = false; partialIds = new Set(); _iterator3 = _createForOfIteratorHelper(messages); _context6.prev = 11; _iterator3.s(); case 13: if ((_step3 = _iterator3.n()).done) { _context6.next = 29; break; } msg = _step3.value; if (!(msg.operation === 'delete' || msg.action === 'delete')) { _context6.next = 18; break; } if ((_msg$ids = msg.ids) !== null && _msg$ids !== void 0 && _msg$ids.length) { deleteIds.push.apply(deleteIds, _toConsumableArray(msg.ids)); } else if (msg.id != null) { deleteIds.push(msg.id); } return _context6.abrupt("continue", 27); case 18: body = msg.body; hasBody = body && _typeof(body) === 'object' && Object.keys(body).length > 0 && body.id != null; if (!hasBody) { _context6.next = 23; break; } bodyItems.push(body); return _context6.abrupt("continue", 27); case 23: if (!(msg.ids && msg.ids.length > 1)) { _context6.next = 26; break; } needFullList = true; return _context6.abrupt("continue", 27); case 26: if (((_msg$ids2 = msg.ids) === null || _msg$ids2 === void 0 ? void 0 : _msg$ids2.length) === 1) { partialIds.add(msg.ids[0]); } else if (msg.id != null) { partialIds.add(msg.id); } case 27: _context6.next = 13; break; case 29: _context6.next = 34; break; case 31: _context6.prev = 31; _context6.t0 = _context6["catch"](11); _iterator3.e(_context6.t0); case 34: _context6.prev = 34; _iterator3.f(); return _context6.finish(34); case 37: uniqueDeletes = _toConsumableArray(new Set(deleteIds)); if (uniqueDeletes.length > 0) { this.removeByIds(uniqueDeletes); changed = true; } if (bodyItems.length > 0) { this.mergeItems(bodyItems); changed = true; } if (!needFullList) { _context6.next = 48; break; } _context6.next = 43; return this.loadListFromServer(); case 43: list = _context6.sent; this.replaceStore(list); changed = true; _context6.next = 70; break; case 48: if (!(partialIds.size > 0)) { _context6.next = 70; break; } fetched = []; _iterator4 = _createForOfIteratorHelper(partialIds); _context6.prev = 51; _iterator4.s(); case 53: if ((_step4 = _iterator4.n()).done) { _context6.next = 61; break; } id = _step4.value; _context6.next = 57; return this.fetchByIdFromServer(id); case 57: one = _context6.sent; if (one) fetched.push(one); case 59: _context6.next = 53; break; case 61: _context6.next = 66; break; case 63: _context6.prev = 63; _context6.t1 = _context6["catch"](51); _iterator4.e(_context6.t1); case 66: _context6.prev = 66; _iterator4.f(); return _context6.finish(66); case 69: if (fetched.length > 0) { this.mergeItems(fetched); changed = true; } case 70: if (changed) { this.core.effects.emit(FloorPlanHooks.onFloorPlansChanged, this.getSortedList()); this.core.effects.emit(FloorPlanHooks.onFloorPlanSyncCompleted, null); } case 71: case "end": return _context6.stop(); } }, _callee6, this, [[11, 31, 34, 37], [51, 63, 66, 69]]); })); function processFloorPlanSyncMessages() { return _processFloorPlanSyncMessages.apply(this, arguments); } return processFloorPlanSyncMessages; }() }, { key: "clear", value: function () { var _clear = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7() { return _regeneratorRuntime().wrap(function _callee7$(_context7) { while (1) switch (_context7.prev = _context7.next) { case 0: this.store.list = []; this.store.map.clear(); this.store.codeMap.clear(); case 3: case "end": return _context7.stop(); } }, _callee7, this); })); function clear() { return _clear.apply(this, arguments); } return clear; }() }, { key: "destroy", value: function destroy() { var _this$floorPlanDataSo; if (this.syncTimer) { clearTimeout(this.syncTimer); this.syncTimer = undefined; } this.pendingSyncMessages = []; if ((_this$floorPlanDataSo = this.floorPlanDataSource) !== null && _this$floorPlanDataSo !== void 0 && _this$floorPlanDataSo.destroy) { this.floorPlanDataSource.destroy(); } _get(_getPrototypeOf(FloorPlanModule.prototype), "destroy", this).call(this); } }]); return FloorPlanModule; }(BaseModule);