Skip to content

Commit 256faac

Browse files
committed
ref: Move check to _loadAndCheckSession
1 parent 984d26d commit 256faac

File tree

7 files changed

+30
-29
lines changed

7 files changed

+30
-29
lines changed

packages/replay/src/replay.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class ReplayContainer implements ReplayContainerInterface {
149149
public start(): void {
150150
this._setInitialState();
151151

152-
this._loadSession({ expiry: SESSION_IDLE_DURATION });
152+
this._loadSession();
153153

154154
// If there is no session, then something bad has happened - can't continue
155155
if (!this.session) {
@@ -258,8 +258,7 @@ export class ReplayContainer implements ReplayContainerInterface {
258258
* new DOM checkout.`
259259
*/
260260
public resume(): void {
261-
if (!this.session || !this.session.sampled) {
262-
this.stop();
261+
if (!this._loadAndCheckSession()) {
263262
return;
264263
}
265264

@@ -309,12 +308,7 @@ export class ReplayContainer implements ReplayContainerInterface {
309308
if (!this._stopRecording) {
310309
// Create a new session, otherwise when the user action is flushed, it
311310
// will get rejected due to an expired session.
312-
this._loadSession({ expiry: SESSION_IDLE_DURATION });
313-
314-
// We know this is set, because it is always set in _loadSession
315-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
316-
if (!this.session!.sampled) {
317-
this.stop();
311+
if (!this._loadAndCheckSession()) {
318312
return;
319313
}
320314

@@ -354,7 +348,7 @@ export class ReplayContainer implements ReplayContainerInterface {
354348
* Returns true if session is not expired, false otherwise.
355349
* @hidden
356350
*/
357-
public checkAndHandleExpiredSession({ expiry = SESSION_IDLE_DURATION }: { expiry?: number } = {}): boolean | void {
351+
public checkAndHandleExpiredSession(expiry?: number): boolean | void {
358352
const oldSessionId = this.getSessionId();
359353

360354
// Prevent starting a new session if the last user activity is older than
@@ -369,12 +363,7 @@ export class ReplayContainer implements ReplayContainerInterface {
369363

370364
// --- There is recent user activity --- //
371365
// This will create a new session if expired, based on expiry length
372-
this._loadSession({ expiry });
373-
374-
// We know this is set, because it is always set in _loadSession
375-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
376-
if (!this.session!.sampled) {
377-
this.stop();
366+
if (!this._loadAndCheckSession(expiry)) {
378367
return;
379368
}
380369

@@ -404,7 +393,7 @@ export class ReplayContainer implements ReplayContainerInterface {
404393
* Loads a session from storage, or creates a new one if it does not exist or
405394
* is expired.
406395
*/
407-
private _loadSession({ expiry }: { expiry: number }): void {
396+
private _loadSession(expiry = SESSION_IDLE_DURATION): void {
408397
const { type, session } = getSession({
409398
expiry,
410399
stickySession: Boolean(this._options.stickySession),
@@ -630,9 +619,7 @@ export class ReplayContainer implements ReplayContainerInterface {
630619
return;
631620
}
632621

633-
const isSessionActive = this.checkAndHandleExpiredSession({
634-
expiry: VISIBILITY_CHANGE_TIMEOUT,
635-
});
622+
const isSessionActive = this.checkAndHandleExpiredSession(VISIBILITY_CHANGE_TIMEOUT);
636623

637624
if (!isSessionActive) {
638625
// If the user has come back to the page within VISIBILITY_CHANGE_TIMEOUT
@@ -891,4 +878,18 @@ export class ReplayContainer implements ReplayContainerInterface {
891878
}, rateLimitDuration);
892879
}
893880
}
881+
882+
/** Loads (or refreshes) the current session. Returns false if session is not recorded. */
883+
private _loadAndCheckSession(expiry?: number): boolean {
884+
this._loadSession(expiry);
885+
886+
// We know this is set, because it is always set in _loadSession
887+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
888+
if (!this.session!.sampled) {
889+
this.stop();
890+
return false;
891+
}
892+
893+
return true;
894+
}
894895
}

packages/replay/test/integration/events.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('Integration | events', () => {
4040
// Create a new session and clear mocks because a segment (from initial
4141
// checkout) will have already been uploaded by the time the tests run
4242
clearSession(replay);
43-
replay['_loadSession']({ expiry: 0 });
43+
replay['_loadSession'](0);
4444
mockTransportSend.mockClear();
4545
});
4646

@@ -93,7 +93,7 @@ describe('Integration | events', () => {
9393

9494
it('has correct timestamps when there are events earlier than initial timestamp', async function () {
9595
clearSession(replay);
96-
replay['_loadSession']({ expiry: 0 });
96+
replay['_loadSession'](0);
9797
mockTransportSend.mockClear();
9898
Object.defineProperty(document, 'visibilityState', {
9999
configurable: true,

packages/replay/test/integration/flush.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('Integration | flush', () => {
9595
jest.setSystemTime(new Date(BASE_TIMESTAMP));
9696
sessionStorage.clear();
9797
clearSession(replay);
98-
replay['_loadSession']({ expiry: SESSION_IDLE_DURATION });
98+
replay['_loadSession'](SESSION_IDLE_DURATION);
9999
mockRecord.takeFullSnapshot.mockClear();
100100
Object.defineProperty(WINDOW, 'location', {
101101
value: prevLocation,

packages/replay/test/integration/rateLimiting.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('Integration | rate-limiting behaviour', () => {
4646
// Create a new session and clear mocks because a segment (from initial
4747
// checkout) will have already been uploaded by the time the tests run
4848
clearSession(replay);
49-
replay['_loadSession']({ expiry: 0 });
49+
replay['_loadSession'](0);
5050

5151
mockSendReplayRequest.mockClear();
5252
});
@@ -57,7 +57,7 @@ describe('Integration | rate-limiting behaviour', () => {
5757
jest.setSystemTime(new Date(BASE_TIMESTAMP));
5858
clearSession(replay);
5959
jest.clearAllMocks();
60-
replay['_loadSession']({ expiry: SESSION_IDLE_DURATION });
60+
replay['_loadSession'](SESSION_IDLE_DURATION);
6161
});
6262

6363
afterAll(() => {

packages/replay/test/integration/sendReplayEvent.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('Integration | sendReplayEvent', () => {
5959
// Create a new session and clear mocks because a segment (from initial
6060
// checkout) will have already been uploaded by the time the tests run
6161
clearSession(replay);
62-
replay['_loadSession']({ expiry: 0 });
62+
replay['_loadSession'](0);
6363

6464
mockSendReplayRequest.mockClear();
6565
});
@@ -69,7 +69,7 @@ describe('Integration | sendReplayEvent', () => {
6969
await new Promise(process.nextTick);
7070
jest.setSystemTime(new Date(BASE_TIMESTAMP));
7171
clearSession(replay);
72-
replay['_loadSession']({ expiry: SESSION_IDLE_DURATION });
72+
replay['_loadSession'](SESSION_IDLE_DURATION);
7373
});
7474

7575
afterAll(() => {

packages/replay/test/integration/session.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ describe('Integration | session', () => {
451451

452452
it('increases segment id after each event', async () => {
453453
clearSession(replay);
454-
replay['_loadSession']({ expiry: 0 });
454+
replay['_loadSession'](0);
455455

456456
Object.defineProperty(document, 'visibilityState', {
457457
configurable: true,

packages/replay/test/integration/stop.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('Integration | stop', () => {
4444
jest.setSystemTime(new Date(BASE_TIMESTAMP));
4545
sessionStorage.clear();
4646
clearSession(replay);
47-
replay['_loadSession']({ expiry: SESSION_IDLE_DURATION });
47+
replay['_loadSession'](SESSION_IDLE_DURATION);
4848
mockRecord.takeFullSnapshot.mockClear();
4949
mockAddInstrumentationHandler.mockClear();
5050
Object.defineProperty(WINDOW, 'location', {

0 commit comments

Comments
 (0)