@@ -10,19 +10,19 @@ const SESSION_ACTIVITY_COUNT_KEY = 'sessionActivitySendCount';
10
10
const SESSION_ACTIVITY_DAY_KEY = 'sessionActivityDate' ;
11
11
12
12
// Maintain a few local counters for session activity
13
- let checkCount = 0 ;
14
- let sendCount = 0 ;
15
- let currentSessionTrackCall = false ;
13
+ const checkCount : { [ k : string ] : number } = { } ;
14
+ let sendCount : { [ k : string ] : number } = { } ;
15
+ const currentSessionTrackCall : { [ k : string ] : boolean } = { } ;
16
16
17
17
// Sync sendCount to localStorage
18
18
const syncSendCount = ( ) => {
19
- sendCount = getItem ( SESSION_ACTIVITY_COUNT_KEY ) || 0 ;
19
+ sendCount = getItem ( SESSION_ACTIVITY_COUNT_KEY ) || { } ;
20
20
const sendDay = getItem ( SESSION_ACTIVITY_DAY_KEY ) ;
21
21
22
22
// If no day, set count to zero. If not today, reset sendCount to 0
23
23
const today = new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] ;
24
24
if ( ! sendDay || sendDay !== today ) {
25
- sendCount = 0 ;
25
+ sendCount = { } ;
26
26
}
27
27
28
28
setItem ( SESSION_ACTIVITY_DAY_KEY , today ) ;
@@ -31,12 +31,12 @@ const syncSendCount = () => {
31
31
// Run as soon as module initialised.
32
32
syncSendCount ( ) ;
33
33
34
- const incrementSendCount = ( ) => {
34
+ const incrementSendCount = ( clientId : string ) => {
35
35
syncSendCount ( ) ;
36
- sendCount ++ ;
36
+ sendCount [ clientId ] ++ ;
37
37
setItem ( SESSION_ACTIVITY_COUNT_KEY , sendCount ) ;
38
38
// Reset checkCount to zero on sending
39
- checkCount = 0 ;
39
+ checkCount [ clientId ] = 0 ;
40
40
} ;
41
41
42
42
// Fix no-promise-executor-return
@@ -47,12 +47,17 @@ const wait = async (seconds: number) => new Promise((resolve) => {
47
47
const trackSessionActivityFn = async ( args : AccountsRequestedEvent ) => {
48
48
// Use an existing flow if one is provided, or create a new one
49
49
const flow = args . flow || trackFlow ( 'passport' , 'sendSessionActivity' ) ;
50
+ const clientId = args . passportClient ;
51
+ if ( ! clientId ) {
52
+ flow . addEvent ( 'No Passport Client ID' ) ;
53
+ throw new Error ( 'No Passport Client ID provided' ) ;
54
+ }
50
55
// If there is already a tracking call in progress, do nothing
51
- if ( currentSessionTrackCall ) {
56
+ if ( currentSessionTrackCall [ clientId ] ) {
52
57
flow . addEvent ( 'Existing Delay Early Exit' ) ;
53
58
return ;
54
59
}
55
- currentSessionTrackCall = true ;
60
+ currentSessionTrackCall [ clientId ] = true ;
56
61
57
62
const { sendTransaction, environment } = args ;
58
63
if ( ! sendTransaction ) {
@@ -64,12 +69,6 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
64
69
}
65
70
setupClient ( environment ) ;
66
71
67
- const clientId = args . passportClient ;
68
- if ( ! clientId ) {
69
- flow . addEvent ( 'No Passport Client ID' ) ;
70
- throw new Error ( 'No Passport Client ID provided' ) ;
71
- }
72
-
73
72
const from = args . walletAddress ;
74
73
if ( ! from ) {
75
74
flow . addEvent ( 'No Passport Wallet Address' ) ;
@@ -84,11 +83,11 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
84
83
details = await get ( {
85
84
clientId,
86
85
wallet : from ,
87
- checkCount,
88
- sendCount,
86
+ checkCount : checkCount [ clientId ] || 0 ,
87
+ sendCount : sendCount [ clientId ] || 0 ,
89
88
} ) ;
90
- checkCount ++ ;
91
- flow . addEvent ( 'Fetched details' , { checkCount } ) ;
89
+ checkCount [ clientId ] ++ ;
90
+ flow . addEvent ( 'Fetched details' , { checkCount : checkCount [ clientId ] } ) ;
92
91
93
92
if ( ! details ) {
94
93
flow . addEvent ( 'No details found' ) ;
@@ -108,7 +107,7 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
108
107
try {
109
108
flow . addEvent ( 'Start Sending Transaction' ) ;
110
109
const tx = await args . sendTransaction ( [ { to, from, data } ] , flow ) ;
111
- incrementSendCount ( ) ;
110
+ incrementSendCount ( clientId ) ;
112
111
flow . addEvent ( 'Transaction Sent' , { tx } ) ;
113
112
} catch ( error ) {
114
113
flow . addEvent ( 'Failed to send Transaction' ) ;
@@ -123,7 +122,7 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
123
122
await wait ( details . delay ) ;
124
123
setTimeout ( ( ) => {
125
124
flow . addEvent ( 'Retrying after Delay' ) ;
126
- currentSessionTrackCall = false ;
125
+ currentSessionTrackCall [ clientId ] = false ;
127
126
// eslint-disable-next-line
128
127
trackSessionWrapper ( { ...args , flow } ) ;
129
128
} , 0 ) ;
@@ -132,7 +131,7 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
132
131
133
132
// Wrapper design to ensure that after track function is called, current session Track call is false.
134
133
const trackSessionWrapper = ( args : AccountsRequestedEvent ) => errorBoundary ( trackSessionActivityFn ) ( args ) . then ( ( ) => {
135
- currentSessionTrackCall = false ;
134
+ currentSessionTrackCall [ args . passportClient ] = false ;
136
135
} ) ;
137
136
138
137
export const trackSessionActivity = trackSessionWrapper ;
0 commit comments