@jetthai/cuora 1.1.366-dev-1 → 1.1.366-dev-2

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.
@@ -3,7 +3,7 @@ import { assign, createMachine } from 'xstate'
3
3
  import auth from '../api/auth.js'
4
4
  import user from '../api/user.js'
5
5
  import storage from '../util/storage.js'
6
- import { getDeviceToken } from '../util/deviceToken.js'
6
+ import { getDeviceTokenWithRetry } from '../util/deviceToken.js'
7
7
  import common from '../api/common.js'
8
8
  import pickBy from 'lodash/pickBy.js'
9
9
 
@@ -432,7 +432,7 @@ export default function (request) {
432
432
  const { provider, channel, subchannel, fc } = event.data
433
433
  const deviceTokenEnable = context.siteConfig?.dtVerify
434
434
 
435
- const dt = await getDeviceToken({
435
+ const dt = await getDeviceTokenWithRetry({
436
436
  fpToken,
437
437
  sentry,
438
438
  hosts: request.hosts,
@@ -517,8 +517,11 @@ export default function (request) {
517
517
  if (deviceTokenEnable) {
518
518
  const { exist } = await request.send(user.checkDt())
519
519
  if (!exist) {
520
- const dt = await getDeviceToken({ fpToken, sentry, hosts: request.hosts, enable: deviceTokenEnable })
521
- await request.send(user.updateDt({ dt }))
520
+ const dt = await getDeviceTokenWithRetry({ fpToken, sentry, hosts: request.hosts, enable: deviceTokenEnable })
521
+ // 確定有dt再取得updateDt
522
+ if (dt) {
523
+ await request.send(user.updateDt({ dt }))
524
+ }
522
525
  }
523
526
  }
524
527
  },
@@ -1,6 +1,6 @@
1
1
  import { assign, createMachine } from 'xstate'
2
2
  import auth from '../api/auth.js'
3
- import { getDeviceToken } from '../util/deviceToken.js'
3
+ import { getDeviceTokenWithRetry } from '../util/deviceToken.js'
4
4
  import storage from '../util/storage.js'
5
5
 
6
6
  export default function (request) {
@@ -200,7 +200,7 @@ export default function (request) {
200
200
  services: {
201
201
  authenticate: async (context, event) => {
202
202
  const { channel, subchannel, signUpData, deviceTokenEnable } = context
203
- const dt = await getDeviceToken({ fpToken, sentry, hosts: request.hosts, enable: deviceTokenEnable })
203
+ const dt = await getDeviceTokenWithRetry({ fpToken, sentry, hosts: request.hosts, enable: deviceTokenEnable })
204
204
 
205
205
  const affiliateCode = storage.get('ac') || signUpData?.affiliateCode
206
206
  const messages = {
@@ -1,5 +1,5 @@
1
1
  import uniqBy from 'lodash/uniqBy'
2
- import { load, defaultEndpoint } from '../lib/fingerprint_v3_11_5.js'
2
+ import { load, defaultEndpoint } from '../lib/fingerprint_v3_11_12.js'
3
3
 
4
4
  const reg = /fnapi\d*/
5
5
 
@@ -32,4 +32,28 @@ const getDeviceToken = async ({ fpToken, sentry, hosts, enable = true }) => {
32
32
  }
33
33
  }
34
34
 
35
- export { getDeviceToken }
35
+ // 重試機制,最多嘗試 maxRetries 次,每次間隔 intervalMs 毫秒
36
+ const getDeviceTokenWithRetry = async ({ fpToken, sentry, hosts, enable = true, maxRetries = 3, intervalMs = 1000 }) => {
37
+ if (!enable) {
38
+ return
39
+ }
40
+ // 設置幾秒後重試
41
+ const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
42
+ // 設定重試次數
43
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
44
+ try {
45
+ const deviceToken = await getDeviceToken({ fpToken, sentry, hosts, enable })
46
+ if (deviceToken) return deviceToken
47
+ throw new Error('No device token received')
48
+ } catch (error) {
49
+ // 若失敗則等待intervalMs後重試,最多重試maxRetries次
50
+ if (attempt < maxRetries) {
51
+ await delay(intervalMs)
52
+ } else {
53
+ throw error
54
+ }
55
+ }
56
+ }
57
+ }
58
+
59
+ export { getDeviceToken, getDeviceTokenWithRetry }
package/src/util/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { getDeviceToken } from './deviceToken.js'
1
+ import { getDeviceToken, getDeviceTokenWithRetry } from './deviceToken.js'
2
2
  import storage from './storage.js'
3
3
  import * as constant from './constant.js'
4
4
  import { max } from './math.js'
5
5
 
6
- export default { getDeviceToken }
6
+ export default { getDeviceToken, getDeviceTokenWithRetry }
7
7
  export { constant, storage, max }