@pisell/pisellos 0.0.222 → 0.0.224

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -651,6 +651,8 @@ export var BookingTicketImpl = /*#__PURE__*/function (_BaseModule) {
651
651
  (_this$store$order = this.store.order) === null || _this$store$order === void 0 || _this$store$order.destroy();
652
652
  this.core.effects.offByModuleDestroy(this.name);
653
653
  this.core.unregisterModule(this);
654
+ // 清除所有的监听
655
+ this.scan.removeAllListeners();
654
656
  // 清除scan数据内存缓存
655
657
  scanCache.clear();
656
658
  }
@@ -18,6 +18,20 @@ export default class Scan {
18
18
  private watchKey;
19
19
  private listenerConfig;
20
20
  constructor(solution: BookingTicketImpl, watchKey: string);
21
+ /**
22
+ * 处理扫码事件
23
+ * @param eventKey 事件key
24
+ * @param value 扫码结果
25
+ */
26
+ handleScan(result: {
27
+ type: string;
28
+ value: string;
29
+ }): void;
30
+ /**
31
+ * 生成uuid
32
+ * @returns uuid
33
+ */
34
+ generateUuid(): string;
21
35
  /**
22
36
  * 添加扫码信息监听
23
37
  * @param watchKey 监听的key
@@ -32,6 +46,10 @@ export default class Scan {
32
46
  * @param event 事件
33
47
  */
34
48
  removeListener(event: WatchEventItem): void;
49
+ /**
50
+ * 移除所有监听
51
+ */
52
+ removeAllListeners(): void;
35
53
  /**
36
54
  * 启用所有监听
37
55
  */
@@ -45,15 +63,6 @@ export default class Scan {
45
63
  * @param eventKey 事件key
46
64
  */
47
65
  clearTaskQueue(eventKey?: string): void;
48
- /**
49
- * 处理扫码事件
50
- * @param eventKey 事件key
51
- * @param value 扫码结果
52
- */
53
- handleScan(value: {
54
- type: string;
55
- value: string;
56
- }): void;
57
66
  /**
58
67
  * 获取监听配置
59
68
  * @param eventKey 事件key
@@ -64,9 +73,9 @@ export default class Scan {
64
73
  task: Tasks;
65
74
  } | undefined;
66
75
  /**
67
- * 处理扫码结果
76
+ * 扫码任务执行函数
68
77
  * @param args 参数
69
78
  */
70
- handleScanResultFn(...args: any[]): Promise<void>;
79
+ scanTaskAction(...args: any[]): Promise<void>;
71
80
  }
72
81
  export {};
@@ -41,11 +41,45 @@ var Scan = /*#__PURE__*/function () {
41
41
  }
42
42
 
43
43
  /**
44
- * 添加扫码信息监听
45
- * @param watchKey 监听的key
46
- * @param event 事件
44
+ * 处理扫码事件
45
+ * @param eventKey 事件key
46
+ * @param value 扫码结果
47
47
  */
48
48
  _createClass(Scan, [{
49
+ key: "handleScan",
50
+ value: function handleScan(result) {
51
+ var lastEnableEventKey = watch.getLastEnableEventKey(this.watchKey);
52
+ if (lastEnableEventKey) {
53
+ var listenerConfig = this.listenerConfig.get(lastEnableEventKey);
54
+ if (listenerConfig) {
55
+ listenerConfig.task.addTask({
56
+ uuid: this.generateUuid(),
57
+ type: lastEnableEventKey,
58
+ actionParams: {
59
+ scanResult: result
60
+ }
61
+ });
62
+ }
63
+ }
64
+ }
65
+
66
+ /**
67
+ * 生成uuid
68
+ * @returns uuid
69
+ */
70
+ }, {
71
+ key: "generateUuid",
72
+ value: function generateUuid() {
73
+ // 需要加入时间戳,避免重复
74
+ return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) + Date.now().toString(36);
75
+ }
76
+
77
+ /**
78
+ * 添加扫码信息监听
79
+ * @param watchKey 监听的key
80
+ * @param event 事件
81
+ */
82
+ }, {
49
83
  key: "addListener",
50
84
  value: function addListener(event, scanCallback) {
51
85
  var _this2 = this;
@@ -57,7 +91,7 @@ var Scan = /*#__PURE__*/function () {
57
91
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
58
92
  args[_key] = arguments[_key];
59
93
  }
60
- return _this2.handleScanResultFn.apply(_this2, [event.key].concat(args));
94
+ return _this2.scanTaskAction.apply(_this2, [event.key].concat(args));
61
95
  });
62
96
 
63
97
  // 创建监听配置
@@ -82,10 +116,25 @@ var Scan = /*#__PURE__*/function () {
82
116
  value: function removeListener(event) {
83
117
  var _this$listenerConfig$;
84
118
  watch.unsubscribe(this.watchKey, event);
85
- (_this$listenerConfig$ = this.listenerConfig.get(event.key)) === null || _this$listenerConfig$ === void 0 || _this$listenerConfig$.task.clear();
119
+ (_this$listenerConfig$ = this.listenerConfig.get(event.key)) === null || _this$listenerConfig$ === void 0 || (_this$listenerConfig$ = _this$listenerConfig$.task) === null || _this$listenerConfig$ === void 0 || _this$listenerConfig$.clear();
86
120
  this.listenerConfig.delete(event.key);
87
121
  }
88
122
 
123
+ /**
124
+ * 移除所有监听
125
+ */
126
+ }, {
127
+ key: "removeAllListeners",
128
+ value: function removeAllListeners() {
129
+ var _this$listenerConfig, _this$listenerConfig$2, _this$listenerConfig2, _this$listenerConfig3;
130
+ watch.clearEvent(this.watchKey);
131
+ (_this$listenerConfig = this.listenerConfig) === null || _this$listenerConfig === void 0 || (_this$listenerConfig$2 = _this$listenerConfig.forEach) === null || _this$listenerConfig$2 === void 0 || _this$listenerConfig$2.call(_this$listenerConfig, function (item) {
132
+ var _item$task;
133
+ item === null || item === void 0 || (_item$task = item.task) === null || _item$task === void 0 || _item$task.clear();
134
+ });
135
+ (_this$listenerConfig2 = this.listenerConfig) === null || _this$listenerConfig2 === void 0 || (_this$listenerConfig3 = _this$listenerConfig2.clear) === null || _this$listenerConfig3 === void 0 || _this$listenerConfig3.call(_this$listenerConfig2);
136
+ }
137
+
89
138
  /**
90
139
  * 启用所有监听
91
140
  */
@@ -101,12 +150,12 @@ var Scan = /*#__PURE__*/function () {
101
150
  }, {
102
151
  key: "disableAllListeners",
103
152
  value: function disableAllListeners() {
104
- var _this$listenerConfig, _this$listenerConfig$2;
153
+ var _this$listenerConfig4, _this$listenerConfig5;
105
154
  watch.disabledAllEventByWatchKey(this.watchKey);
106
155
  // 清空任务
107
- (_this$listenerConfig = this.listenerConfig) === null || _this$listenerConfig === void 0 || (_this$listenerConfig$2 = _this$listenerConfig.forEach) === null || _this$listenerConfig$2 === void 0 || _this$listenerConfig$2.call(_this$listenerConfig, function (item) {
108
- var _item$task;
109
- item === null || item === void 0 || (_item$task = item.task) === null || _item$task === void 0 || _item$task.clearTaskQueue();
156
+ (_this$listenerConfig4 = this.listenerConfig) === null || _this$listenerConfig4 === void 0 || (_this$listenerConfig5 = _this$listenerConfig4.forEach) === null || _this$listenerConfig5 === void 0 || _this$listenerConfig5.call(_this$listenerConfig4, function (item) {
157
+ var _item$task2;
158
+ item === null || item === void 0 || (_item$task2 = item.task) === null || _item$task2 === void 0 || _item$task2.clearTaskQueue();
110
159
  });
111
160
  }
112
161
 
@@ -117,40 +166,19 @@ var Scan = /*#__PURE__*/function () {
117
166
  }, {
118
167
  key: "clearTaskQueue",
119
168
  value: function clearTaskQueue(eventKey) {
169
+ console.log('clearTaskQueue>>>>>>>', eventKey);
120
170
  if (eventKey) {
121
171
  var _this$listenerConfig$3;
122
172
  (_this$listenerConfig$3 = this.listenerConfig.get(eventKey)) === null || _this$listenerConfig$3 === void 0 || _this$listenerConfig$3.task.clearTaskQueue();
123
173
  } else {
124
- var _this$listenerConfig2, _this$listenerConfig3;
125
- (_this$listenerConfig2 = this.listenerConfig) === null || _this$listenerConfig2 === void 0 || (_this$listenerConfig3 = _this$listenerConfig2.forEach) === null || _this$listenerConfig3 === void 0 || _this$listenerConfig3.call(_this$listenerConfig2, function (item) {
126
- var _item$task2;
127
- item === null || item === void 0 || (_item$task2 = item.task) === null || _item$task2 === void 0 || _item$task2.clearTaskQueue();
174
+ var _this$listenerConfig6, _this$listenerConfig7;
175
+ (_this$listenerConfig6 = this.listenerConfig) === null || _this$listenerConfig6 === void 0 || (_this$listenerConfig7 = _this$listenerConfig6.forEach) === null || _this$listenerConfig7 === void 0 || _this$listenerConfig7.call(_this$listenerConfig6, function (item) {
176
+ var _item$task3;
177
+ item === null || item === void 0 || (_item$task3 = item.task) === null || _item$task3 === void 0 || _item$task3.clearTaskQueue();
128
178
  });
129
179
  }
130
180
  }
131
181
 
132
- /**
133
- * 处理扫码事件
134
- * @param eventKey 事件key
135
- * @param value 扫码结果
136
- */
137
- }, {
138
- key: "handleScan",
139
- value: function handleScan(value) {
140
- var lastEnableEventKey = watch.getLastEnableEventKey(this.watchKey);
141
- if (lastEnableEventKey) {
142
- var listenerConfig = this.listenerConfig.get(lastEnableEventKey);
143
- if (listenerConfig) {
144
- listenerConfig.task.addTask({
145
- type: lastEnableEventKey,
146
- actionParams: {
147
- scanResult: value
148
- }
149
- });
150
- }
151
- }
152
- }
153
-
154
182
  /**
155
183
  * 获取监听配置
156
184
  * @param eventKey 事件key
@@ -159,19 +187,18 @@ var Scan = /*#__PURE__*/function () {
159
187
  }, {
160
188
  key: "getListenerConfig",
161
189
  value: function getListenerConfig(eventKey) {
162
- var _this$listenerConfig4;
163
- return (_this$listenerConfig4 = this.listenerConfig) === null || _this$listenerConfig4 === void 0 ? void 0 : _this$listenerConfig4.get(eventKey);
190
+ var _this$listenerConfig8;
191
+ return (_this$listenerConfig8 = this.listenerConfig) === null || _this$listenerConfig8 === void 0 ? void 0 : _this$listenerConfig8.get(eventKey);
164
192
  }
165
193
 
166
194
  /**
167
- * 处理扫码结果
195
+ * 扫码任务执行函数
168
196
  * @param args 参数
169
197
  */
170
198
  }, {
171
- key: "handleScanResultFn",
199
+ key: "scanTaskAction",
172
200
  value: (function () {
173
- var _handleScanResultFn = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
174
- var _this$getListenerConf;
201
+ var _scanTaskAction = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
175
202
  var _len2,
176
203
  args,
177
204
  _key2,
@@ -181,7 +208,10 @@ var Scan = /*#__PURE__*/function () {
181
208
  type,
182
209
  value,
183
210
  data,
211
+ taskItemUuid,
212
+ _ref2,
184
213
  scanCallback,
214
+ task,
185
215
  _result2,
186
216
  _args = arguments;
187
217
  return _regeneratorRuntime().wrap(function _callee$(_context) {
@@ -192,51 +222,62 @@ var Scan = /*#__PURE__*/function () {
192
222
  }
193
223
  eventKey = args[0], other = args[1];
194
224
  _ref = (other === null || other === void 0 ? void 0 : other.scanResult) || {}, type = _ref.type, value = _ref.value, data = _ref.data;
195
- scanCallback = (_this$getListenerConf = this.getListenerConfig(eventKey)) === null || _this$getListenerConf === void 0 ? void 0 : _this$getListenerConf.scanCallback;
225
+ taskItemUuid = other === null || other === void 0 ? void 0 : other.uuid;
226
+ _ref2 = this.getListenerConfig(eventKey) || {}, scanCallback = _ref2.scanCallback, task = _ref2.task;
196
227
  if (!scanCallback) {
197
- _context.next = 17;
228
+ _context.next = 18;
198
229
  break;
199
230
  }
200
- _context.prev = 5;
201
- _context.next = 8;
231
+ _context.prev = 6;
232
+ _context.next = 9;
202
233
  return scanCallback({
203
234
  type: type,
204
235
  value: value
205
236
  });
206
- case 8:
237
+ case 9:
207
238
  _result2 = _context.sent;
208
239
  console.log("".concat(eventKey, "_result>>>>>>>"), _result2);
209
- watch.publishByEventKey(this.watchKey, eventKey, {
210
- type: eventKey,
211
- status: 'success',
212
- scanType: type,
213
- scanCode: value,
214
- scanData: data,
215
- result: _result2
216
- });
217
- _context.next = 17;
240
+ // 如果任务存在,则发布结果
241
+ if (taskItemUuid && task !== null && task !== void 0 && task.isTaskExist(taskItemUuid)) {
242
+ watch.publishByEventKey(this.watchKey, eventKey, {
243
+ type: eventKey,
244
+ status: 'success',
245
+ scanType: type,
246
+ scanCode: value,
247
+ scanData: data,
248
+ result: _result2
249
+ });
250
+ } else {
251
+ console.warn("".concat(eventKey, "_result_task_not_exist>>>>>>>"), task);
252
+ }
253
+ _context.next = 18;
218
254
  break;
219
- case 13:
220
- _context.prev = 13;
221
- _context.t0 = _context["catch"](5);
255
+ case 14:
256
+ _context.prev = 14;
257
+ _context.t0 = _context["catch"](6);
222
258
  console.log("".concat(eventKey, "_result_error>>>>>>>"), _context.t0);
223
- watch.publishByEventKey(this.watchKey, eventKey, {
224
- type: eventKey,
225
- status: 'failed',
226
- scanType: type,
227
- scanCode: value,
228
- scanData: data
229
- });
230
- case 17:
259
+ // 如果任务存在,则发布结果
260
+ if (taskItemUuid && task !== null && task !== void 0 && task.isTaskExist(taskItemUuid)) {
261
+ watch.publishByEventKey(this.watchKey, eventKey, {
262
+ type: eventKey,
263
+ status: 'failed',
264
+ scanType: type,
265
+ scanCode: value,
266
+ scanData: data
267
+ });
268
+ } else {
269
+ console.warn("".concat(eventKey, "_result_error_task_not_exist>>>>>>>"), task);
270
+ }
271
+ case 18:
231
272
  case "end":
232
273
  return _context.stop();
233
274
  }
234
- }, _callee, this, [[5, 13]]);
275
+ }, _callee, this, [[6, 14]]);
235
276
  }));
236
- function handleScanResultFn() {
237
- return _handleScanResultFn.apply(this, arguments);
277
+ function scanTaskAction() {
278
+ return _scanTaskAction.apply(this, arguments);
238
279
  }
239
- return handleScanResultFn;
280
+ return scanTaskAction;
240
281
  }())
241
282
  }]);
242
283
  return Scan;
@@ -1,5 +1,6 @@
1
1
  type Action = (...args: any[]) => Promise<any>;
2
2
  type Task = {
3
+ uuid: string;
3
4
  type: string;
4
5
  actionParams?: Record<string, any>;
5
6
  };
@@ -31,5 +32,9 @@ declare class Tasks {
31
32
  * 执行任务
32
33
  */
33
34
  run(): Promise<void>;
35
+ /**
36
+ * 判定当前的任务是否依然存在
37
+ */
38
+ isTaskExist(uuid: string): boolean;
34
39
  }
35
40
  export default Tasks;
@@ -1,5 +1,7 @@
1
1
  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); }
2
2
  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; }
3
+ 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; }
4
+ 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; }
3
5
  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); } }
4
6
  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); }); }; }
5
7
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
@@ -70,7 +72,7 @@ var Tasks = /*#__PURE__*/function () {
70
72
  key: "run",
71
73
  value: (function () {
72
74
  var _run = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
73
- var task, action;
75
+ var _this$taskQueue, task, action, _task, _task2;
74
76
  return _regeneratorRuntime().wrap(function _callee$(_context) {
75
77
  while (1) switch (_context.prev = _context.next) {
76
78
  case 0:
@@ -92,11 +94,11 @@ var Tasks = /*#__PURE__*/function () {
92
94
  // 遍历任务队列
93
95
  case 6:
94
96
  if (!(this.taskQueue.length > 0)) {
95
- _context.next = 17;
97
+ _context.next = 32;
96
98
  break;
97
99
  }
98
- // 获取任务
99
- task = this.taskQueue.shift(); // 如果没有任务则直接结束
100
+ // 获取任务,但是不删除
101
+ task = (_this$taskQueue = this.taskQueue) === null || _this$taskQueue === void 0 ? void 0 : _this$taskQueue[0]; // 如果没有任务则直接结束
100
102
  if (task) {
101
103
  _context.next = 11;
102
104
  break;
@@ -107,27 +109,62 @@ var Tasks = /*#__PURE__*/function () {
107
109
  // 获取任务对应的action
108
110
  action = this.actions.get(task.type); // 如果找到对应的action,则执行
109
111
  if (!action) {
110
- _context.next = 15;
112
+ _context.next = 28;
111
113
  break;
112
114
  }
113
- _context.next = 15;
114
- return action((task === null || task === void 0 ? void 0 : task.actionParams) || {});
115
- case 15:
115
+ _context.prev = 13;
116
+ _context.next = 16;
117
+ return action(_objectSpread(_objectSpread({}, (task === null || task === void 0 ? void 0 : task.actionParams) || {}), {}, {
118
+ uuid: task === null || task === void 0 ? void 0 : task.uuid
119
+ }));
120
+ case 16:
121
+ console.log('task_success>>>>>>>', task);
122
+ _context.next = 22;
123
+ break;
124
+ case 19:
125
+ _context.prev = 19;
126
+ _context.t0 = _context["catch"](13);
127
+ console.error('task_error>>>>>>>', _context.t0);
128
+ case 22:
129
+ _context.prev = 22;
130
+ // 将任务删除
131
+ _task = this.taskQueue.shift();
132
+ console.log('task_shift>>>>>>>', _task);
133
+ return _context.finish(22);
134
+ case 26:
135
+ _context.next = 30;
136
+ break;
137
+ case 28:
138
+ // 如果找不到对应的action,则删除任务
139
+ _task2 = this.taskQueue.shift();
140
+ console.log('task_action_not_found>>>>>>>', _task2);
141
+ case 30:
116
142
  _context.next = 6;
117
143
  break;
118
- case 17:
144
+ case 32:
119
145
  this.isRunning = false;
120
- case 18:
146
+ case 33:
121
147
  case "end":
122
148
  return _context.stop();
123
149
  }
124
- }, _callee, this);
150
+ }, _callee, this, [[13, 19, 22, 26]]);
125
151
  }));
126
152
  function run() {
127
153
  return _run.apply(this, arguments);
128
154
  }
129
155
  return run;
130
- }())
156
+ }()
157
+ /**
158
+ * 判定当前的任务是否依然存在
159
+ */
160
+ )
161
+ }, {
162
+ key: "isTaskExist",
163
+ value: function isTaskExist(uuid) {
164
+ return this.taskQueue.some(function (task) {
165
+ return (task === null || task === void 0 ? void 0 : task.uuid) === uuid;
166
+ });
167
+ }
131
168
  }]);
132
169
  return Tasks;
133
170
  }();
@@ -387,6 +387,7 @@ var BookingTicketImpl = class extends import_BaseModule.BaseModule {
387
387
  (_e = this.store.order) == null ? void 0 : _e.destroy();
388
388
  this.core.effects.offByModuleDestroy(this.name);
389
389
  this.core.unregisterModule(this);
390
+ this.scan.removeAllListeners();
390
391
  import_scanCache.default.clear();
391
392
  }
392
393
  };
@@ -18,6 +18,20 @@ export default class Scan {
18
18
  private watchKey;
19
19
  private listenerConfig;
20
20
  constructor(solution: BookingTicketImpl, watchKey: string);
21
+ /**
22
+ * 处理扫码事件
23
+ * @param eventKey 事件key
24
+ * @param value 扫码结果
25
+ */
26
+ handleScan(result: {
27
+ type: string;
28
+ value: string;
29
+ }): void;
30
+ /**
31
+ * 生成uuid
32
+ * @returns uuid
33
+ */
34
+ generateUuid(): string;
21
35
  /**
22
36
  * 添加扫码信息监听
23
37
  * @param watchKey 监听的key
@@ -32,6 +46,10 @@ export default class Scan {
32
46
  * @param event 事件
33
47
  */
34
48
  removeListener(event: WatchEventItem): void;
49
+ /**
50
+ * 移除所有监听
51
+ */
52
+ removeAllListeners(): void;
35
53
  /**
36
54
  * 启用所有监听
37
55
  */
@@ -45,15 +63,6 @@ export default class Scan {
45
63
  * @param eventKey 事件key
46
64
  */
47
65
  clearTaskQueue(eventKey?: string): void;
48
- /**
49
- * 处理扫码事件
50
- * @param eventKey 事件key
51
- * @param value 扫码结果
52
- */
53
- handleScan(value: {
54
- type: string;
55
- value: string;
56
- }): void;
57
66
  /**
58
67
  * 获取监听配置
59
68
  * @param eventKey 事件key
@@ -64,9 +73,9 @@ export default class Scan {
64
73
  task: Tasks;
65
74
  } | undefined;
66
75
  /**
67
- * 处理扫码结果
76
+ * 扫码任务执行函数
68
77
  * @param args 参数
69
78
  */
70
- handleScanResultFn(...args: any[]): Promise<void>;
79
+ scanTaskAction(...args: any[]): Promise<void>;
71
80
  }
72
81
  export {};
@@ -55,6 +55,33 @@ var Scan = class {
55
55
  }
56
56
  );
57
57
  }
58
+ /**
59
+ * 处理扫码事件
60
+ * @param eventKey 事件key
61
+ * @param value 扫码结果
62
+ */
63
+ handleScan(result) {
64
+ const lastEnableEventKey = import_watch.default.getLastEnableEventKey(this.watchKey);
65
+ if (lastEnableEventKey) {
66
+ const listenerConfig = this.listenerConfig.get(lastEnableEventKey);
67
+ if (listenerConfig) {
68
+ listenerConfig.task.addTask({
69
+ uuid: this.generateUuid(),
70
+ type: lastEnableEventKey,
71
+ actionParams: {
72
+ scanResult: result
73
+ }
74
+ });
75
+ }
76
+ }
77
+ }
78
+ /**
79
+ * 生成uuid
80
+ * @returns uuid
81
+ */
82
+ generateUuid() {
83
+ return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) + Date.now().toString(36);
84
+ }
58
85
  /**
59
86
  * 添加扫码信息监听
60
87
  * @param watchKey 监听的key
@@ -65,7 +92,7 @@ var Scan = class {
65
92
  const task = new import_task.default();
66
93
  task.addAction(
67
94
  event.key,
68
- (...args) => this.handleScanResultFn(event.key, ...args)
95
+ (...args) => this.scanTaskAction(event.key, ...args)
69
96
  );
70
97
  this.listenerConfig.set(event.key, { scanCallback, task });
71
98
  return {
@@ -80,11 +107,23 @@ var Scan = class {
80
107
  * @param event 事件
81
108
  */
82
109
  removeListener(event) {
83
- var _a;
110
+ var _a, _b;
84
111
  import_watch.default.unsubscribe(this.watchKey, event);
85
- (_a = this.listenerConfig.get(event.key)) == null ? void 0 : _a.task.clear();
112
+ (_b = (_a = this.listenerConfig.get(event.key)) == null ? void 0 : _a.task) == null ? void 0 : _b.clear();
86
113
  this.listenerConfig.delete(event.key);
87
114
  }
115
+ /**
116
+ * 移除所有监听
117
+ */
118
+ removeAllListeners() {
119
+ var _a, _b, _c, _d;
120
+ import_watch.default.clearEvent(this.watchKey);
121
+ (_b = (_a = this.listenerConfig) == null ? void 0 : _a.forEach) == null ? void 0 : _b.call(_a, (item) => {
122
+ var _a2;
123
+ (_a2 = item == null ? void 0 : item.task) == null ? void 0 : _a2.clear();
124
+ });
125
+ (_d = (_c = this.listenerConfig) == null ? void 0 : _c.clear) == null ? void 0 : _d.call(_c);
126
+ }
88
127
  /**
89
128
  * 启用所有监听
90
129
  */
@@ -108,6 +147,7 @@ var Scan = class {
108
147
  */
109
148
  clearTaskQueue(eventKey) {
110
149
  var _a, _b, _c;
150
+ console.log("clearTaskQueue>>>>>>>", eventKey);
111
151
  if (eventKey) {
112
152
  (_a = this.listenerConfig.get(eventKey)) == null ? void 0 : _a.task.clearTaskQueue();
113
153
  } else {
@@ -117,25 +157,6 @@ var Scan = class {
117
157
  });
118
158
  }
119
159
  }
120
- /**
121
- * 处理扫码事件
122
- * @param eventKey 事件key
123
- * @param value 扫码结果
124
- */
125
- handleScan(value) {
126
- const lastEnableEventKey = import_watch.default.getLastEnableEventKey(this.watchKey);
127
- if (lastEnableEventKey) {
128
- const listenerConfig = this.listenerConfig.get(lastEnableEventKey);
129
- if (listenerConfig) {
130
- listenerConfig.task.addTask({
131
- type: lastEnableEventKey,
132
- actionParams: {
133
- scanResult: value
134
- }
135
- });
136
- }
137
- }
138
- }
139
160
  /**
140
161
  * 获取监听配置
141
162
  * @param eventKey 事件key
@@ -146,35 +167,43 @@ var Scan = class {
146
167
  return (_a = this.listenerConfig) == null ? void 0 : _a.get(eventKey);
147
168
  }
148
169
  /**
149
- * 处理扫码结果
170
+ * 扫码任务执行函数
150
171
  * @param args 参数
151
172
  */
152
- async handleScanResultFn(...args) {
153
- var _a;
173
+ async scanTaskAction(...args) {
154
174
  const [eventKey, other] = args;
155
175
  const { type, value, data } = (other == null ? void 0 : other.scanResult) || {};
156
- const scanCallback = (_a = this.getListenerConfig(eventKey)) == null ? void 0 : _a.scanCallback;
176
+ const taskItemUuid = other == null ? void 0 : other.uuid;
177
+ const { scanCallback, task } = this.getListenerConfig(eventKey) || {};
157
178
  if (scanCallback) {
158
179
  try {
159
180
  const result = await scanCallback({ type, value });
160
181
  console.log(`${eventKey}_result>>>>>>>`, result);
161
- import_watch.default.publishByEventKey(this.watchKey, eventKey, {
162
- type: eventKey,
163
- status: "success",
164
- scanType: type,
165
- scanCode: value,
166
- scanData: data,
167
- result
168
- });
182
+ if (taskItemUuid && (task == null ? void 0 : task.isTaskExist(taskItemUuid))) {
183
+ import_watch.default.publishByEventKey(this.watchKey, eventKey, {
184
+ type: eventKey,
185
+ status: "success",
186
+ scanType: type,
187
+ scanCode: value,
188
+ scanData: data,
189
+ result
190
+ });
191
+ } else {
192
+ console.warn(`${eventKey}_result_task_not_exist>>>>>>>`, task);
193
+ }
169
194
  } catch (error) {
170
195
  console.log(`${eventKey}_result_error>>>>>>>`, error);
171
- import_watch.default.publishByEventKey(this.watchKey, eventKey, {
172
- type: eventKey,
173
- status: "failed",
174
- scanType: type,
175
- scanCode: value,
176
- scanData: data
177
- });
196
+ if (taskItemUuid && (task == null ? void 0 : task.isTaskExist(taskItemUuid))) {
197
+ import_watch.default.publishByEventKey(this.watchKey, eventKey, {
198
+ type: eventKey,
199
+ status: "failed",
200
+ scanType: type,
201
+ scanCode: value,
202
+ scanData: data
203
+ });
204
+ } else {
205
+ console.warn(`${eventKey}_result_error_task_not_exist>>>>>>>`, task);
206
+ }
178
207
  }
179
208
  }
180
209
  }
@@ -1,5 +1,6 @@
1
1
  type Action = (...args: any[]) => Promise<any>;
2
2
  type Task = {
3
+ uuid: string;
3
4
  type: string;
4
5
  actionParams?: Record<string, any>;
5
6
  };
@@ -31,5 +32,9 @@ declare class Tasks {
31
32
  * 执行任务
32
33
  */
33
34
  run(): Promise<void>;
35
+ /**
36
+ * 判定当前的任务是否依然存在
37
+ */
38
+ isTaskExist(uuid: string): boolean;
34
39
  }
35
40
  export default Tasks;
package/lib/utils/task.js CHANGED
@@ -66,6 +66,7 @@ var Tasks = class {
66
66
  * 执行任务
67
67
  */
68
68
  async run() {
69
+ var _a;
69
70
  if (this.isRunning) {
70
71
  return;
71
72
  }
@@ -75,17 +76,34 @@ var Tasks = class {
75
76
  }
76
77
  this.isRunning = true;
77
78
  while (this.taskQueue.length > 0) {
78
- const task = this.taskQueue.shift();
79
+ const task = (_a = this.taskQueue) == null ? void 0 : _a[0];
79
80
  if (!task) {
80
81
  this.isRunning = false;
81
82
  return;
82
83
  }
83
84
  const action = this.actions.get(task.type);
84
85
  if (action) {
85
- await action((task == null ? void 0 : task.actionParams) || {});
86
+ try {
87
+ await action({ ...(task == null ? void 0 : task.actionParams) || {}, uuid: task == null ? void 0 : task.uuid });
88
+ console.log("task_success>>>>>>>", task);
89
+ } catch (error) {
90
+ console.error("task_error>>>>>>>", error);
91
+ } finally {
92
+ const _task = this.taskQueue.shift();
93
+ console.log("task_shift>>>>>>>", _task);
94
+ }
95
+ } else {
96
+ const _task = this.taskQueue.shift();
97
+ console.log("task_action_not_found>>>>>>>", _task);
86
98
  }
87
99
  }
88
100
  this.isRunning = false;
89
101
  }
102
+ /**
103
+ * 判定当前的任务是否依然存在
104
+ */
105
+ isTaskExist(uuid) {
106
+ return this.taskQueue.some((task) => (task == null ? void 0 : task.uuid) === uuid);
107
+ }
90
108
  };
91
109
  var task_default = Tasks;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "0.0.222",
4
+ "version": "0.0.224",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",