@@ -53,7 +53,7 @@ export default class GleapSession {
5353 }
5454 } ;
5555
56- clearSession = ( renewSession = true ) => {
56+ clearSession = ( attemp = 0 ) => {
5757 try {
5858 saveToGleapCache ( `session-${ this . sdkKey } ` , null ) ;
5959 } catch ( exp ) { }
@@ -68,9 +68,12 @@ export default class GleapSession {
6868 value : 0 ,
6969 } ;
7070
71- // Start guest session.
72- if ( renewSession ) {
73- this . startSession ( ) ;
71+ if ( ! isNaN ( attemp ) ) {
72+ // Exponentially retry to renew session.
73+ const newTimeout = ( Math . pow ( attemp , 2 ) * 10 ) + 10 ;
74+ setTimeout ( ( ) => {
75+ this . startSession ( attemp + 1 ) ;
76+ } , newTimeout * 1000 ) ;
7477 }
7578 } ;
7679
@@ -87,7 +90,7 @@ export default class GleapSession {
8790 this . notifySessionReady ( ) ;
8891 } ;
8992
90- startSession = ( ) => {
93+ startSession = ( attemp = 0 ) => {
9194 // Check if session is already ready.
9295 const cachedSession = loadFromGleapCache ( `session-${ this . sdkKey } ` ) ;
9396 if ( cachedSession ) {
@@ -105,9 +108,6 @@ export default class GleapSession {
105108 http . setRequestHeader ( "Gleap-Hash" , this . session . gleapHash ) ;
106109 }
107110 } catch ( exp ) { }
108- http . onerror = ( error ) => {
109- self . clearSession ( false ) ;
110- } ;
111111 http . onreadystatechange = function ( e ) {
112112 if ( http . readyState === XMLHttpRequest . DONE ) {
113113 if ( http . status === 200 || http . status === 201 ) {
@@ -116,7 +116,9 @@ export default class GleapSession {
116116 self . validateSession ( sessionData ) ;
117117 } catch ( exp ) { }
118118 } else {
119- self . clearSession ( false ) ;
119+ if ( http . status !== 429 ) {
120+ self . clearSession ( attemp ) ;
121+ }
120122 }
121123 }
122124 } ;
@@ -183,7 +185,6 @@ export default class GleapSession {
183185 } catch ( exp ) { }
184186
185187 http . onerror = ( ) => {
186- self . clearSession ( true ) ;
187188 reject ( ) ;
188189 } ;
189190 http . onreadystatechange = function ( e ) {
@@ -195,11 +196,9 @@ export default class GleapSession {
195196
196197 resolve ( sessionData ) ;
197198 } catch ( exp ) {
198- self . clearSession ( true ) ;
199199 reject ( exp ) ;
200200 }
201201 } else {
202- self . clearSession ( true ) ;
203202 reject ( ) ;
204203 }
205204 }
0 commit comments