@@ -10,19 +10,19 @@ const SESSION_ACTIVITY_COUNT_KEY = 'sessionActivitySendCount';
1010const SESSION_ACTIVITY_DAY_KEY = 'sessionActivityDate' ;
1111
1212// 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 } = { } ;
1616
1717// Sync sendCount to localStorage
1818const syncSendCount = ( ) => {
19- sendCount = getItem ( SESSION_ACTIVITY_COUNT_KEY ) || 0 ;
19+ sendCount = getItem ( SESSION_ACTIVITY_COUNT_KEY ) || { } ;
2020 const sendDay = getItem ( SESSION_ACTIVITY_DAY_KEY ) ;
2121
2222 // If no day, set count to zero. If not today, reset sendCount to 0
2323 const today = new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] ;
2424 if ( ! sendDay || sendDay !== today ) {
25- sendCount = 0 ;
25+ sendCount = { } ;
2626 }
2727
2828 setItem ( SESSION_ACTIVITY_DAY_KEY , today ) ;
@@ -31,12 +31,12 @@ const syncSendCount = () => {
3131// Run as soon as module initialised.
3232syncSendCount ( ) ;
3333
34- const incrementSendCount = ( ) => {
34+ const incrementSendCount = ( clientId : string ) => {
3535 syncSendCount ( ) ;
36- sendCount ++ ;
36+ sendCount [ clientId ] ++ ;
3737 setItem ( SESSION_ACTIVITY_COUNT_KEY , sendCount ) ;
3838 // Reset checkCount to zero on sending
39- checkCount = 0 ;
39+ checkCount [ clientId ] = 0 ;
4040} ;
4141
4242// Fix no-promise-executor-return
@@ -47,12 +47,17 @@ const wait = async (seconds: number) => new Promise((resolve) => {
4747const trackSessionActivityFn = async ( args : AccountsRequestedEvent ) => {
4848 // Use an existing flow if one is provided, or create a new one
4949 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+ }
5055 // If there is already a tracking call in progress, do nothing
51- if ( currentSessionTrackCall ) {
56+ if ( currentSessionTrackCall [ clientId ] ) {
5257 flow . addEvent ( 'Existing Delay Early Exit' ) ;
5358 return ;
5459 }
55- currentSessionTrackCall = true ;
60+ currentSessionTrackCall [ clientId ] = true ;
5661
5762 const { sendTransaction, environment } = args ;
5863 if ( ! sendTransaction ) {
@@ -64,12 +69,6 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
6469 }
6570 setupClient ( environment ) ;
6671
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-
7372 const from = args . walletAddress ;
7473 if ( ! from ) {
7574 flow . addEvent ( 'No Passport Wallet Address' ) ;
@@ -84,11 +83,11 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
8483 details = await get ( {
8584 clientId,
8685 wallet : from ,
87- checkCount,
88- sendCount,
86+ checkCount : checkCount [ clientId ] || 0 ,
87+ sendCount : sendCount [ clientId ] || 0 ,
8988 } ) ;
90- checkCount ++ ;
91- flow . addEvent ( 'Fetched details' , { checkCount } ) ;
89+ checkCount [ clientId ] ++ ;
90+ flow . addEvent ( 'Fetched details' , { checkCount : checkCount [ clientId ] } ) ;
9291
9392 if ( ! details ) {
9493 flow . addEvent ( 'No details found' ) ;
@@ -108,7 +107,7 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
108107 try {
109108 flow . addEvent ( 'Start Sending Transaction' ) ;
110109 const tx = await args . sendTransaction ( [ { to, from, data } ] , flow ) ;
111- incrementSendCount ( ) ;
110+ incrementSendCount ( clientId ) ;
112111 flow . addEvent ( 'Transaction Sent' , { tx } ) ;
113112 } catch ( error ) {
114113 flow . addEvent ( 'Failed to send Transaction' ) ;
@@ -123,7 +122,7 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
123122 await wait ( details . delay ) ;
124123 setTimeout ( ( ) => {
125124 flow . addEvent ( 'Retrying after Delay' ) ;
126- currentSessionTrackCall = false ;
125+ currentSessionTrackCall [ clientId ] = false ;
127126 // eslint-disable-next-line
128127 trackSessionWrapper ( { ...args , flow } ) ;
129128 } , 0 ) ;
@@ -132,7 +131,7 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
132131
133132// Wrapper design to ensure that after track function is called, current session Track call is false.
134133const trackSessionWrapper = ( args : AccountsRequestedEvent ) => errorBoundary ( trackSessionActivityFn ) ( args ) . then ( ( ) => {
135- currentSessionTrackCall = false ;
134+ currentSessionTrackCall [ args . passportClient ] = false ;
136135} ) ;
137136
138137export const trackSessionActivity = trackSessionWrapper ;
0 commit comments