@ray-js/lock-sdk 1.1.3-beta.4 → 1.1.3-beta.6
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.
- package/lib/unlock-method.js +51 -10
- package/package.json +1 -1
package/lib/unlock-method.js
CHANGED
|
@@ -225,12 +225,50 @@ const addUnlockMethodData = {
|
|
|
225
225
|
userId: "",
|
|
226
226
|
unlockDpId: 0,
|
|
227
227
|
timeout: 15000,
|
|
228
|
+
canEmitEvents: true,
|
|
229
|
+
pendingEvents: [],
|
|
230
|
+
releaseEmitTimer: 0,
|
|
231
|
+
eventSessionId: 0,
|
|
232
|
+
};
|
|
233
|
+
const ADD_UNLOCK_METHOD_EVENT_RELEASE_DELAY = 16;
|
|
234
|
+
const dispatchAddUnlockMethodEvent = (eventData) => {
|
|
235
|
+
if (emitter.hasListener(UNLOCK_METHOD_EVENT)) {
|
|
236
|
+
emitter.emit(UNLOCK_METHOD_EVENT, eventData);
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
emitter.cache(UNLOCK_METHOD_EVENT, eventData);
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
const emitAddUnlockMethodEvent = (eventData) => {
|
|
243
|
+
if (!addUnlockMethodData.canEmitEvents) {
|
|
244
|
+
addUnlockMethodData.pendingEvents.push(eventData);
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
dispatchAddUnlockMethodEvent(eventData);
|
|
248
|
+
};
|
|
249
|
+
const releaseAddUnlockMethodEventsAfterStart = (eventSessionId) => {
|
|
250
|
+
clearTimeout(addUnlockMethodData.releaseEmitTimer);
|
|
251
|
+
addUnlockMethodData.releaseEmitTimer = setTimeout(() => {
|
|
252
|
+
if (addUnlockMethodData.eventSessionId !== eventSessionId) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
addUnlockMethodData.releaseEmitTimer = setTimeout(() => {
|
|
256
|
+
if (addUnlockMethodData.eventSessionId !== eventSessionId) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
addUnlockMethodData.canEmitEvents = true;
|
|
260
|
+
const pendingEvents = addUnlockMethodData.pendingEvents.splice(0);
|
|
261
|
+
pendingEvents.forEach((eventData) => {
|
|
262
|
+
dispatchAddUnlockMethodEvent(eventData);
|
|
263
|
+
});
|
|
264
|
+
}, ADD_UNLOCK_METHOD_EVENT_RELEASE_DELAY);
|
|
265
|
+
}, 0);
|
|
228
266
|
};
|
|
229
267
|
const handleAddTimeout = (timeout = 15000) => {
|
|
230
268
|
clearTimeout(addUnlockMethodData.timeoutId);
|
|
231
269
|
addUnlockMethodData.timeoutId = setTimeout(() => {
|
|
232
270
|
emitter.off(DPCHANGE, handleAddReport);
|
|
233
|
-
|
|
271
|
+
emitAddUnlockMethodEvent({
|
|
234
272
|
type: addUnlockMethodData.unlockMethodConfig.type,
|
|
235
273
|
stage: "fail",
|
|
236
274
|
lockUserId: addUnlockMethodData.lockUserId,
|
|
@@ -357,12 +395,7 @@ const handleAddReport = async (dps) => {
|
|
|
357
395
|
default:
|
|
358
396
|
}
|
|
359
397
|
if (eventData) {
|
|
360
|
-
|
|
361
|
-
emitter.emit(UNLOCK_METHOD_EVENT, eventData);
|
|
362
|
-
}
|
|
363
|
-
else {
|
|
364
|
-
emitter.cache(UNLOCK_METHOD_EVENT, eventData);
|
|
365
|
-
}
|
|
398
|
+
emitAddUnlockMethodEvent(eventData);
|
|
366
399
|
}
|
|
367
400
|
}
|
|
368
401
|
};
|
|
@@ -372,21 +405,27 @@ const clearMonitoringAddReport = () => {
|
|
|
372
405
|
};
|
|
373
406
|
const monitoringAddReport = (option) => {
|
|
374
407
|
Object.assign(addUnlockMethodData, option);
|
|
408
|
+
clearTimeout(addUnlockMethodData.releaseEmitTimer);
|
|
409
|
+
addUnlockMethodData.canEmitEvents = false;
|
|
410
|
+
addUnlockMethodData.pendingEvents = [];
|
|
411
|
+
addUnlockMethodData.eventSessionId += 1;
|
|
375
412
|
emitter.clearCache(UNLOCK_METHOD_EVENT);
|
|
376
413
|
emitter.off(DPCHANGE, handleAddReport);
|
|
377
414
|
emitter.on(DPCHANGE, handleAddReport);
|
|
378
415
|
handleAddTimeout(option.timeout);
|
|
416
|
+
return addUnlockMethodData.eventSessionId;
|
|
379
417
|
};
|
|
380
418
|
export const startAddUnlockMethod = async (params) => {
|
|
381
419
|
const { unlockMethodConfig, lockUserId, dpData, sn, addDpCode, addDpMap, addDpReportMap, dpId, } = await getUnlockMethodBase(params.type, params.userId);
|
|
382
420
|
isCancelAddUnlockMethod = false;
|
|
383
421
|
let addReportMonitoringStarted = false;
|
|
422
|
+
let addReportMonitoringSessionId = 0;
|
|
384
423
|
const startAddReportMonitoring = (total) => {
|
|
385
424
|
if (addReportMonitoringStarted) {
|
|
386
|
-
return;
|
|
425
|
+
return addReportMonitoringSessionId;
|
|
387
426
|
}
|
|
388
427
|
addReportMonitoringStarted = true;
|
|
389
|
-
monitoringAddReport({
|
|
428
|
+
addReportMonitoringSessionId = monitoringAddReport({
|
|
390
429
|
timeout: params.timeout,
|
|
391
430
|
unlockMethodConfig,
|
|
392
431
|
total,
|
|
@@ -397,6 +436,7 @@ export const startAddUnlockMethod = async (params) => {
|
|
|
397
436
|
userId: params.userId,
|
|
398
437
|
unlockDpId: dpId,
|
|
399
438
|
});
|
|
439
|
+
return addReportMonitoringSessionId;
|
|
400
440
|
};
|
|
401
441
|
const res = await publishDps({
|
|
402
442
|
[addDpCode]: dpUtils.format(dpData, addDpMap),
|
|
@@ -429,7 +469,8 @@ export const startAddUnlockMethod = async (params) => {
|
|
|
429
469
|
},
|
|
430
470
|
});
|
|
431
471
|
if (res.stage === 0) {
|
|
432
|
-
startAddReportMonitoring(res.total);
|
|
472
|
+
const eventSessionId = startAddReportMonitoring(res.total);
|
|
473
|
+
releaseAddUnlockMethodEventsAfterStart(eventSessionId);
|
|
433
474
|
return { total: res.total };
|
|
434
475
|
}
|
|
435
476
|
if (res.stage === 0xfd) {
|