Skip to content

Commit 496d1f1

Browse files
committed
v14.0.7
Optimized partial update.
1 parent 7169ee1 commit 496d1f1

File tree

6 files changed

+103
-42
lines changed

6 files changed

+103
-42
lines changed

build/cjs/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/esm/index.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gleap",
3-
"version": "14.0.6",
3+
"version": "14.0.7",
44
"main": "build/cjs/index.js",
55
"module": "build/esm/index.mjs",
66
"exports": {

published/14.0.7/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

published/latest/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/GleapSession.js

Lines changed: 98 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
import { GleapEventManager, GleapTranslationManager, GleapFrameManager, GleapNotificationManager, GleapStreamedEvent, GleapBannerManager } from "./Gleap";
2-
import { eraseGleapCookie, getGleapCookie, loadFromGleapCache, saveToGleapCache, setGleapCookie } from "./GleapHelper";
1+
import {
2+
GleapEventManager,
3+
GleapTranslationManager,
4+
GleapFrameManager,
5+
GleapNotificationManager,
6+
GleapStreamedEvent,
7+
GleapBannerManager,
8+
} from "./Gleap";
9+
import {
10+
eraseGleapCookie,
11+
getGleapCookie,
12+
loadFromGleapCache,
13+
saveToGleapCache,
14+
setGleapCookie,
15+
} from "./GleapHelper";
316
import GleapTooltipManager from "./GleapTooltipManager";
417

518
export default class GleapSession {
@@ -15,7 +28,7 @@ export default class GleapSession {
1528
email: "",
1629
userId: "",
1730
phone: "",
18-
value: 0
31+
value: 0,
1932
};
2033
ready = false;
2134
onSessionReadyListener = [];
@@ -37,23 +50,29 @@ export default class GleapSession {
3750
*/
3851
getName() {
3952
try {
40-
return this.session.name ? this.session.name.split(' ')[0].split('@')[0].split('.')[0].split('+')[0] : "";
53+
return this.session.name
54+
? this.session.name
55+
.split(" ")[0]
56+
.split("@")[0]
57+
.split(".")[0]
58+
.split("+")[0]
59+
: "";
4160
} catch (exp) {
4261
return this.session.name;
4362
}
4463
}
4564

4665
/**
4766
* Returns the Gleap session object.
48-
* @returns
67+
* @returns
4968
*/
5069
getSession() {
5170
return this.session;
5271
}
5372

5473
/**
5574
* Returns the Gleap session object.
56-
* @returns
75+
* @returns
5776
*/
5877
getGleapId() {
5978
if (this.session && this.session.gleapId) {
@@ -74,7 +93,7 @@ export default class GleapSession {
7493
return false;
7594
}
7695

77-
constructor() { }
96+
constructor() {}
7897

7998
setOnSessionReady = (onSessionReady) => {
8099
if (this.ready) {
@@ -94,17 +113,20 @@ export default class GleapSession {
94113

95114
clearSession = (attemp = 0, retry = true) => {
96115
if (this.session && this.session.gleapHash) {
97-
GleapEventManager.notifyEvent("unregister-pushmessage-group", `gleapuser-${this.session.gleapHash}`);
116+
GleapEventManager.notifyEvent(
117+
"unregister-pushmessage-group",
118+
`gleapuser-${this.session.gleapHash}`
119+
);
98120
}
99121

100122
try {
101123
saveToGleapCache(`session-${this.sdkKey}`, null);
102-
} catch (exp) { }
124+
} catch (exp) {}
103125

104126
if (this.useCookies) {
105127
try {
106128
eraseGleapCookie(`session-${this.sdkKey}`);
107-
} catch (exp) { }
129+
} catch (exp) {}
108130
}
109131

110132
this.ready = false;
@@ -115,20 +137,23 @@ export default class GleapSession {
115137
email: "",
116138
userId: "",
117139
phone: "",
118-
value: 0
140+
value: 0,
119141
};
120142

121-
GleapFrameManager.getInstance().sendMessage({
122-
name: "session-cleared"
123-
}, true);
143+
GleapFrameManager.getInstance().sendMessage(
144+
{
145+
name: "session-cleared",
146+
},
147+
true
148+
);
124149
GleapNotificationManager.getInstance().clearAllNotifications(false);
125150
GleapNotificationManager.getInstance().setNotificationCount(0);
126151
GleapBannerManager.getInstance().removeBannerUI();
127152

128153
if (retry) {
129154
if (!isNaN(attemp)) {
130155
// Exponentially retry to renew session.
131-
const newTimeout = (Math.pow(attemp, 2) * 10);
156+
const newTimeout = Math.pow(attemp, 2) * 10;
132157
setTimeout(() => {
133158
this.startSession(attemp + 1);
134159
}, newTimeout * 1000);
@@ -143,20 +168,30 @@ export default class GleapSession {
143168

144169
// Unregister previous group.
145170
if (this.session && this.session.gleapHash) {
146-
GleapEventManager.notifyEvent("unregister-pushmessage-group", `gleapuser-${this.session.gleapHash}`);
171+
GleapEventManager.notifyEvent(
172+
"unregister-pushmessage-group",
173+
`gleapuser-${this.session.gleapHash}`
174+
);
147175
}
148176

149177
saveToGleapCache(`session-${this.sdkKey}`, session);
150178
if (this.useCookies) {
151-
setGleapCookie(`session-${this.sdkKey}`, encodeURIComponent(JSON.stringify(session)), 365);
179+
setGleapCookie(
180+
`session-${this.sdkKey}`,
181+
encodeURIComponent(JSON.stringify(session)),
182+
365
183+
);
152184
}
153185

154186
this.session = session;
155187
this.ready = true;
156188

157189
// Register new push group.
158190
if (this.session && this.session.gleapHash) {
159-
GleapEventManager.notifyEvent("register-pushmessage-group", `gleapuser-${this.session.gleapHash}`);
191+
GleapEventManager.notifyEvent(
192+
"register-pushmessage-group",
193+
`gleapuser-${this.session.gleapHash}`
194+
);
160195
}
161196

162197
this.notifySessionReady();
@@ -172,10 +207,12 @@ export default class GleapSession {
172207
this.validateSession(sessionData);
173208
}
174209
}
175-
} catch (exp) { }
210+
} catch (exp) {}
176211

177212
// Try to load session from local storage, if not already loaded.
178-
if (!(this.session && this.session.gleapId && this.session.gleapId.length > 0)) {
213+
if (
214+
!(this.session && this.session.gleapId && this.session.gleapId.length > 0)
215+
) {
179216
const cachedSession = loadFromGleapCache(`session-${this.sdkKey}`);
180217
if (cachedSession) {
181218
this.validateSession(cachedSession);
@@ -192,7 +229,7 @@ export default class GleapSession {
192229
http.setRequestHeader("Gleap-Id", this.session.gleapId);
193230
http.setRequestHeader("Gleap-Hash", this.session.gleapHash);
194231
}
195-
} catch (exp) { }
232+
} catch (exp) {}
196233
http.onreadystatechange = function (e) {
197234
if (http.readyState === 4) {
198235
if (http.status === 200 || http.status === 201) {
@@ -205,17 +242,19 @@ export default class GleapSession {
205242

206243
// Load tooltips.
207244
GleapTooltipManager.getInstance().load();
208-
} catch (exp) { }
245+
} catch (exp) {}
209246
} else {
210247
if (http.status !== 429) {
211248
self.clearSession(attemp, true);
212249
}
213250
}
214251
}
215252
};
216-
http.send(JSON.stringify({
217-
lang: GleapTranslationManager.getInstance().getActiveLanguage(),
218-
}));
253+
http.send(
254+
JSON.stringify({
255+
lang: GleapTranslationManager.getInstance().getActiveLanguage(),
256+
})
257+
);
219258
};
220259

221260
notifySessionReady() {
@@ -239,15 +278,28 @@ export default class GleapSession {
239278
if (this.session.userId.toString() !== userId.toString()) {
240279
return true;
241280
}
242-
} catch (exp) { }
281+
} catch (exp) {}
243282

283+
return checkIfSessionDataNeedsUpdate(userData);
284+
};
285+
286+
checkIfSessionDataNeedsUpdate = (userData) => {
244287
if (userData) {
245288
var userDataKeys = Object.keys(userData);
246289
for (var i = 0; i < userDataKeys.length; i++) {
247290
var userDataKey = userDataKeys[i];
248-
if (JSON.stringify(this.session[userDataKey]) !== JSON.stringify(userData[userDataKey])) {
291+
if (
292+
JSON.stringify(this.session[userDataKey]) !==
293+
JSON.stringify(userData[userDataKey])
294+
) {
249295
// Check custom data for a match.
250-
if (!(this.session.customData && JSON.stringify(this.session.customData[userDataKey]) === JSON.stringify(userData[userDataKey]))) {
296+
if (
297+
!(
298+
this.session.customData &&
299+
JSON.stringify(this.session.customData[userDataKey]) ===
300+
JSON.stringify(userData[userDataKey])
301+
)
302+
) {
251303
return true;
252304
}
253305
}
@@ -258,6 +310,12 @@ export default class GleapSession {
258310
};
259311

260312
updateSession = (userData) => {
313+
// Check if session needs update.
314+
const sessionNeedsUpdate = this.checkIfSessionDataNeedsUpdate(userData);
315+
if (!sessionNeedsUpdate) {
316+
return;
317+
}
318+
261319
const self = this;
262320
return new Promise((resolve, reject) => {
263321
// Wait for gleap session to be ready.
@@ -273,7 +331,7 @@ export default class GleapSession {
273331
try {
274332
http.setRequestHeader("Gleap-Id", self.session.gleapId);
275333
http.setRequestHeader("Gleap-Hash", self.session.gleapHash);
276-
} catch (exp) { }
334+
} catch (exp) {}
277335

278336
http.onerror = () => {
279337
reject();
@@ -300,7 +358,7 @@ export default class GleapSession {
300358
...userData,
301359
lang: GleapTranslationManager.getInstance().getActiveLanguage(),
302360
},
303-
type: 'js',
361+
type: "js",
304362
sdkVersion: SDK_VERSION,
305363
ws: true,
306364
})
@@ -330,7 +388,7 @@ export default class GleapSession {
330388
try {
331389
http.setRequestHeader("Gleap-Id", self.session.gleapId);
332390
http.setRequestHeader("Gleap-Hash", self.session.gleapHash);
333-
} catch (exp) { }
391+
} catch (exp) {}
334392

335393
http.onerror = () => {
336394
reject();
@@ -358,15 +416,15 @@ export default class GleapSession {
358416
};
359417

360418
var dataToSend = {
361-
...userData
419+
...userData,
362420
};
363421

364422
if (userData.customData) {
365-
delete dataToSend['customData'];
423+
delete dataToSend["customData"];
366424
dataToSend = {
367425
...dataToSend,
368426
...userData.customData,
369-
}
427+
};
370428
}
371429

372430
http.send(
@@ -396,7 +454,7 @@ export default class GleapSession {
396454
try {
397455
http.setRequestHeader("Gleap-Id", self.session.gleapId);
398456
http.setRequestHeader("Gleap-Hash", self.session.gleapHash);
399-
} catch (exp) { }
457+
} catch (exp) {}
400458

401459
http.onerror = () => {
402460
reject();
@@ -417,9 +475,11 @@ export default class GleapSession {
417475
}
418476
}
419477
};
420-
http.send(JSON.stringify({
421-
outboundId: tourId
422-
}));
478+
http.send(
479+
JSON.stringify({
480+
outboundId: tourId,
481+
})
482+
);
423483
});
424484
});
425485
};

0 commit comments

Comments
 (0)