@pisell/pisellos 0.0.223 → 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.
@@ -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
@@ -49,15 +63,6 @@ export default class Scan {
49
63
  * @param eventKey 事件key
50
64
  */
51
65
  clearTaskQueue(eventKey?: string): void;
52
- /**
53
- * 处理扫码事件
54
- * @param eventKey 事件key
55
- * @param value 扫码结果
56
- */
57
- handleScan(value: {
58
- type: string;
59
- value: string;
60
- }): void;
61
66
  /**
62
67
  * 获取监听配置
63
68
  * @param eventKey 事件key
@@ -68,9 +73,9 @@ export default class Scan {
68
73
  task: Tasks;
69
74
  } | undefined;
70
75
  /**
71
- * 处理扫码结果
76
+ * 扫码任务执行函数
72
77
  * @param args 参数
73
78
  */
74
- handleScanResultFn(...args: any[]): Promise<void>;
79
+ scanTaskAction(...args: any[]): Promise<void>;
75
80
  }
76
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
  // 创建监听配置
@@ -132,6 +166,7 @@ var Scan = /*#__PURE__*/function () {
132
166
  }, {
133
167
  key: "clearTaskQueue",
134
168
  value: function clearTaskQueue(eventKey) {
169
+ console.log('clearTaskQueue>>>>>>>', eventKey);
135
170
  if (eventKey) {
136
171
  var _this$listenerConfig$3;
137
172
  (_this$listenerConfig$3 = this.listenerConfig.get(eventKey)) === null || _this$listenerConfig$3 === void 0 || _this$listenerConfig$3.task.clearTaskQueue();
@@ -144,28 +179,6 @@ var Scan = /*#__PURE__*/function () {
144
179
  }
145
180
  }
146
181
 
147
- /**
148
- * 处理扫码事件
149
- * @param eventKey 事件key
150
- * @param value 扫码结果
151
- */
152
- }, {
153
- key: "handleScan",
154
- value: function handleScan(value) {
155
- var lastEnableEventKey = watch.getLastEnableEventKey(this.watchKey);
156
- if (lastEnableEventKey) {
157
- var listenerConfig = this.listenerConfig.get(lastEnableEventKey);
158
- if (listenerConfig) {
159
- listenerConfig.task.addTask({
160
- type: lastEnableEventKey,
161
- actionParams: {
162
- scanResult: value
163
- }
164
- });
165
- }
166
- }
167
- }
168
-
169
182
  /**
170
183
  * 获取监听配置
171
184
  * @param eventKey 事件key
@@ -179,14 +192,13 @@ var Scan = /*#__PURE__*/function () {
179
192
  }
180
193
 
181
194
  /**
182
- * 处理扫码结果
195
+ * 扫码任务执行函数
183
196
  * @param args 参数
184
197
  */
185
198
  }, {
186
- key: "handleScanResultFn",
199
+ key: "scanTaskAction",
187
200
  value: (function () {
188
- var _handleScanResultFn = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
189
- var _this$getListenerConf;
201
+ var _scanTaskAction = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
190
202
  var _len2,
191
203
  args,
192
204
  _key2,
@@ -196,7 +208,10 @@ var Scan = /*#__PURE__*/function () {
196
208
  type,
197
209
  value,
198
210
  data,
211
+ taskItemUuid,
212
+ _ref2,
199
213
  scanCallback,
214
+ task,
200
215
  _result2,
201
216
  _args = arguments;
202
217
  return _regeneratorRuntime().wrap(function _callee$(_context) {
@@ -207,51 +222,62 @@ var Scan = /*#__PURE__*/function () {
207
222
  }
208
223
  eventKey = args[0], other = args[1];
209
224
  _ref = (other === null || other === void 0 ? void 0 : other.scanResult) || {}, type = _ref.type, value = _ref.value, data = _ref.data;
210
- 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;
211
227
  if (!scanCallback) {
212
- _context.next = 17;
228
+ _context.next = 18;
213
229
  break;
214
230
  }
215
- _context.prev = 5;
216
- _context.next = 8;
231
+ _context.prev = 6;
232
+ _context.next = 9;
217
233
  return scanCallback({
218
234
  type: type,
219
235
  value: value
220
236
  });
221
- case 8:
237
+ case 9:
222
238
  _result2 = _context.sent;
223
239
  console.log("".concat(eventKey, "_result>>>>>>>"), _result2);
224
- watch.publishByEventKey(this.watchKey, eventKey, {
225
- type: eventKey,
226
- status: 'success',
227
- scanType: type,
228
- scanCode: value,
229
- scanData: data,
230
- result: _result2
231
- });
232
- _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;
233
254
  break;
234
- case 13:
235
- _context.prev = 13;
236
- _context.t0 = _context["catch"](5);
255
+ case 14:
256
+ _context.prev = 14;
257
+ _context.t0 = _context["catch"](6);
237
258
  console.log("".concat(eventKey, "_result_error>>>>>>>"), _context.t0);
238
- watch.publishByEventKey(this.watchKey, eventKey, {
239
- type: eventKey,
240
- status: 'failed',
241
- scanType: type,
242
- scanCode: value,
243
- scanData: data
244
- });
245
- 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:
246
272
  case "end":
247
273
  return _context.stop();
248
274
  }
249
- }, _callee, this, [[5, 13]]);
275
+ }, _callee, this, [[6, 14]]);
250
276
  }));
251
- function handleScanResultFn() {
252
- return _handleScanResultFn.apply(this, arguments);
277
+ function scanTaskAction() {
278
+ return _scanTaskAction.apply(this, arguments);
253
279
  }
254
- return handleScanResultFn;
280
+ return scanTaskAction;
255
281
  }())
256
282
  }]);
257
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
  }();
@@ -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
@@ -49,15 +63,6 @@ export default class Scan {
49
63
  * @param eventKey 事件key
50
64
  */
51
65
  clearTaskQueue(eventKey?: string): void;
52
- /**
53
- * 处理扫码事件
54
- * @param eventKey 事件key
55
- * @param value 扫码结果
56
- */
57
- handleScan(value: {
58
- type: string;
59
- value: string;
60
- }): void;
61
66
  /**
62
67
  * 获取监听配置
63
68
  * @param eventKey 事件key
@@ -68,9 +73,9 @@ export default class Scan {
68
73
  task: Tasks;
69
74
  } | undefined;
70
75
  /**
71
- * 处理扫码结果
76
+ * 扫码任务执行函数
72
77
  * @param args 参数
73
78
  */
74
- handleScanResultFn(...args: any[]): Promise<void>;
79
+ scanTaskAction(...args: any[]): Promise<void>;
75
80
  }
76
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 {
@@ -120,6 +147,7 @@ var Scan = class {
120
147
  */
121
148
  clearTaskQueue(eventKey) {
122
149
  var _a, _b, _c;
150
+ console.log("clearTaskQueue>>>>>>>", eventKey);
123
151
  if (eventKey) {
124
152
  (_a = this.listenerConfig.get(eventKey)) == null ? void 0 : _a.task.clearTaskQueue();
125
153
  } else {
@@ -129,25 +157,6 @@ var Scan = class {
129
157
  });
130
158
  }
131
159
  }
132
- /**
133
- * 处理扫码事件
134
- * @param eventKey 事件key
135
- * @param value 扫码结果
136
- */
137
- handleScan(value) {
138
- const lastEnableEventKey = import_watch.default.getLastEnableEventKey(this.watchKey);
139
- if (lastEnableEventKey) {
140
- const listenerConfig = this.listenerConfig.get(lastEnableEventKey);
141
- if (listenerConfig) {
142
- listenerConfig.task.addTask({
143
- type: lastEnableEventKey,
144
- actionParams: {
145
- scanResult: value
146
- }
147
- });
148
- }
149
- }
150
- }
151
160
  /**
152
161
  * 获取监听配置
153
162
  * @param eventKey 事件key
@@ -158,35 +167,43 @@ var Scan = class {
158
167
  return (_a = this.listenerConfig) == null ? void 0 : _a.get(eventKey);
159
168
  }
160
169
  /**
161
- * 处理扫码结果
170
+ * 扫码任务执行函数
162
171
  * @param args 参数
163
172
  */
164
- async handleScanResultFn(...args) {
165
- var _a;
173
+ async scanTaskAction(...args) {
166
174
  const [eventKey, other] = args;
167
175
  const { type, value, data } = (other == null ? void 0 : other.scanResult) || {};
168
- 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) || {};
169
178
  if (scanCallback) {
170
179
  try {
171
180
  const result = await scanCallback({ type, value });
172
181
  console.log(`${eventKey}_result>>>>>>>`, result);
173
- import_watch.default.publishByEventKey(this.watchKey, eventKey, {
174
- type: eventKey,
175
- status: "success",
176
- scanType: type,
177
- scanCode: value,
178
- scanData: data,
179
- result
180
- });
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
+ }
181
194
  } catch (error) {
182
195
  console.log(`${eventKey}_result_error>>>>>>>`, error);
183
- import_watch.default.publishByEventKey(this.watchKey, eventKey, {
184
- type: eventKey,
185
- status: "failed",
186
- scanType: type,
187
- scanCode: value,
188
- scanData: data
189
- });
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
+ }
190
207
  }
191
208
  }
192
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.223",
4
+ "version": "0.0.224",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",