Skip to content

Commit e253dab

Browse files
committed
Exponential refresh.
1 parent e51987c commit e253dab

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

demo/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Gleap = window.Gleap;
22

33
// Gleap.setFrameUrl("http://localhost:3000");
44
// Gleap.setLanguage("en");
5+
Gleap.setApiUrl("http://localhost:9000");
56
Gleap.initialize("KProDXhMS0V3UUku2iNnrZ4XsBnAYzxt");
67

78
Gleap.identify("user_19283", {

src/GleapSession.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/GleapStreamedEvent.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,14 @@ export default class GleapStreamedEvent {
8989
http.open("POST", GleapSession.getInstance().apiUrl + "/sessions/stream");
9090
http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
9191
GleapSession.getInstance().injectSession(http);
92-
http.onerror = (error) => {
93-
GleapSession.getInstance().clearSession(true);
94-
};
92+
http.onerror = (error) => {};
9593
http.onreadystatechange = function (e) {
9694
if (http.readyState === XMLHttpRequest.DONE) {
9795
if (http.status === 200 || http.status === 201) {
9896
try {
9997
const action = JSON.parse(http.responseText);
10098
Gleap.getInstance().performAction(action);
10199
} catch (exp) { }
102-
} else {
103-
GleapSession.getInstance().clearSession(true);
104100
}
105101
}
106102
};

0 commit comments

Comments
 (0)