@ray-js/lock-sdk 1.1.3-beta.3 → 1.1.3-beta.5

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.
@@ -225,12 +225,40 @@ const addUnlockMethodData = {
225
225
  userId: "",
226
226
  unlockDpId: 0,
227
227
  timeout: 15000,
228
+ canEmitEvents: true,
229
+ pendingEvents: [],
230
+ releaseEmitTimer: 0,
231
+ };
232
+ const dispatchAddUnlockMethodEvent = (eventData) => {
233
+ if (emitter.hasListener(UNLOCK_METHOD_EVENT)) {
234
+ emitter.emit(UNLOCK_METHOD_EVENT, eventData);
235
+ }
236
+ else {
237
+ emitter.cache(UNLOCK_METHOD_EVENT, eventData);
238
+ }
239
+ };
240
+ const emitAddUnlockMethodEvent = (eventData) => {
241
+ if (!addUnlockMethodData.canEmitEvents) {
242
+ addUnlockMethodData.pendingEvents.push(eventData);
243
+ return;
244
+ }
245
+ dispatchAddUnlockMethodEvent(eventData);
246
+ };
247
+ const releaseAddUnlockMethodEventsAfterStart = () => {
248
+ clearTimeout(addUnlockMethodData.releaseEmitTimer);
249
+ addUnlockMethodData.releaseEmitTimer = setTimeout(() => {
250
+ addUnlockMethodData.canEmitEvents = true;
251
+ const pendingEvents = addUnlockMethodData.pendingEvents.splice(0);
252
+ pendingEvents.forEach((eventData) => {
253
+ dispatchAddUnlockMethodEvent(eventData);
254
+ });
255
+ }, 0);
228
256
  };
229
257
  const handleAddTimeout = (timeout = 15000) => {
230
258
  clearTimeout(addUnlockMethodData.timeoutId);
231
259
  addUnlockMethodData.timeoutId = setTimeout(() => {
232
260
  emitter.off(DPCHANGE, handleAddReport);
233
- emitter.emit(UNLOCK_METHOD_EVENT, {
261
+ emitAddUnlockMethodEvent({
234
262
  type: addUnlockMethodData.unlockMethodConfig.type,
235
263
  stage: "fail",
236
264
  lockUserId: addUnlockMethodData.lockUserId,
@@ -357,12 +385,7 @@ const handleAddReport = async (dps) => {
357
385
  default:
358
386
  }
359
387
  if (eventData) {
360
- if (emitter.hasListener(UNLOCK_METHOD_EVENT)) {
361
- emitter.emit(UNLOCK_METHOD_EVENT, eventData);
362
- }
363
- else {
364
- emitter.cache(UNLOCK_METHOD_EVENT, eventData);
365
- }
388
+ emitAddUnlockMethodEvent(eventData);
366
389
  }
367
390
  }
368
391
  };
@@ -372,6 +395,9 @@ const clearMonitoringAddReport = () => {
372
395
  };
373
396
  const monitoringAddReport = (option) => {
374
397
  Object.assign(addUnlockMethodData, option);
398
+ clearTimeout(addUnlockMethodData.releaseEmitTimer);
399
+ addUnlockMethodData.canEmitEvents = false;
400
+ addUnlockMethodData.pendingEvents = [];
375
401
  emitter.clearCache(UNLOCK_METHOD_EVENT);
376
402
  emitter.off(DPCHANGE, handleAddReport);
377
403
  emitter.on(DPCHANGE, handleAddReport);
@@ -380,6 +406,24 @@ const monitoringAddReport = (option) => {
380
406
  export const startAddUnlockMethod = async (params) => {
381
407
  const { unlockMethodConfig, lockUserId, dpData, sn, addDpCode, addDpMap, addDpReportMap, dpId, } = await getUnlockMethodBase(params.type, params.userId);
382
408
  isCancelAddUnlockMethod = false;
409
+ let addReportMonitoringStarted = false;
410
+ const startAddReportMonitoring = (total) => {
411
+ if (addReportMonitoringStarted) {
412
+ return;
413
+ }
414
+ addReportMonitoringStarted = true;
415
+ monitoringAddReport({
416
+ timeout: params.timeout,
417
+ unlockMethodConfig,
418
+ total,
419
+ dpCode: addDpCode,
420
+ dpMap: addDpReportMap,
421
+ sn,
422
+ lockUserId,
423
+ userId: params.userId,
424
+ unlockDpId: dpId,
425
+ });
426
+ };
383
427
  const res = await publishDps({
384
428
  [addDpCode]: dpUtils.format(dpData, addDpMap),
385
429
  }, {
@@ -400,17 +444,7 @@ export const startAddUnlockMethod = async (params) => {
400
444
  console.warn("AddPassword: An invalid sn may have been reported");
401
445
  }
402
446
  if (result.stage === 0) {
403
- monitoringAddReport({
404
- timeout: params.timeout,
405
- unlockMethodConfig,
406
- total: res.total,
407
- dpCode: addDpCode,
408
- dpMap: addDpReportMap,
409
- sn,
410
- lockUserId,
411
- userId: params.userId,
412
- unlockDpId: dpId,
413
- });
447
+ startAddReportMonitoring(result.total);
414
448
  }
415
449
  return result;
416
450
  }
@@ -421,6 +455,8 @@ export const startAddUnlockMethod = async (params) => {
421
455
  },
422
456
  });
423
457
  if (res.stage === 0) {
458
+ startAddReportMonitoring(res.total);
459
+ releaseAddUnlockMethodEventsAfterStart();
424
460
  return { total: res.total };
425
461
  }
426
462
  if (res.stage === 0xfd) {
@@ -34,17 +34,20 @@ export const publishDpsOnly = async (dpData) => {
34
34
  }
35
35
  };
36
36
  export const publishDps = async (dpData, option) => {
37
- await publishDpsOnly(dpData);
38
37
  const { timeout = 15000, checkReport } = option || {};
39
38
  return new Promise((resolve, reject) => {
40
39
  const codes = Object.keys(dpData);
41
40
  const result = {};
41
+ let timer;
42
+ const cleanup = () => {
43
+ emitter.off(DPCHANGE, handleChangeDp);
44
+ clearTimeout(timer);
45
+ };
42
46
  const handleChangeDp = (dpCodes) => {
43
47
  if (checkReport) {
44
48
  const pass = checkReport(dpCodes);
45
49
  if (pass) {
46
- emitter.off(DPCHANGE, handleChangeDp);
47
- clearTimeout(timer);
50
+ cleanup();
48
51
  resolve(typeof pass !== "boolean" ? pass : dpCodes);
49
52
  }
50
53
  }
@@ -57,16 +60,19 @@ export const publishDps = async (dpData, option) => {
57
60
  }
58
61
  });
59
62
  if (codes.length === 0) {
60
- emitter.off(DPCHANGE, handleChangeDp);
61
- clearTimeout(timer);
63
+ cleanup();
62
64
  resolve(result);
63
65
  }
64
66
  }
65
67
  };
66
- const timer = setTimeout(() => {
67
- emitter.off(DPCHANGE, handleChangeDp);
68
+ timer = setTimeout(() => {
69
+ cleanup();
68
70
  reject(getError(1002));
69
71
  }, timeout);
70
72
  emitter.on(DPCHANGE, handleChangeDp);
73
+ publishDpsOnly(dpData).catch((error) => {
74
+ cleanup();
75
+ reject(error);
76
+ });
71
77
  });
72
78
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/lock-sdk",
3
- "version": "1.1.3-beta.3",
3
+ "version": "1.1.3-beta.5",
4
4
  "files": [
5
5
  "lib",
6
6
  "LICENSE.md"