Skip to content

Commit 2489b45

Browse files
committed
ref: Move check to _loadAndCheckSession
1 parent cc66d0d commit 2489b45

File tree

7 files changed

+31
-31
lines changed

7 files changed

+31
-31
lines changed

packages/replay/src/replay.ts

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

152-
this._loadSession({ expiry: SESSION_IDLE_DURATION });
152+
if (!this._loadAndCheckSession()) {
153+
return;
154+
}
153155

154156
// If there is no session, then something bad has happened - can't continue
155157
if (!this.session) {
@@ -228,6 +230,10 @@ export class ReplayContainer implements ReplayContainerInterface {
228230
* does not support a teardown
229231
*/
230232
public stop(): void {
233+
if (!this._isEnabled) {
234+
return;
235+
}
236+
231237
try {
232238
__DEBUG_BUILD__ && logger.log('[Replay] Stopping Replays');
233239
this._isEnabled = false;
@@ -258,8 +264,7 @@ export class ReplayContainer implements ReplayContainerInterface {
258264
* new DOM checkout.`
259265
*/
260266
public resume(): void {
261-
if (!this.session || !this.session.sampled) {
262-
this.stop();
267+
if (!this._loadAndCheckSession()) {
263268
return;
264269
}
265270

@@ -309,12 +314,7 @@ export class ReplayContainer implements ReplayContainerInterface {
309314
if (!this._stopRecording) {
310315
// Create a new session, otherwise when the user action is flushed, it
311316
// 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();
317+
if (!this._loadAndCheckSession()) {
318318
return;
319319
}
320320

@@ -354,7 +354,7 @@ export class ReplayContainer implements ReplayContainerInterface {
354354
* Returns true if session is not expired, false otherwise.
355355
* @hidden
356356
*/
357-
public checkAndHandleExpiredSession({ expiry = SESSION_IDLE_DURATION }: { expiry?: number } = {}): boolean | void {
357+
public checkAndHandleExpiredSession(expiry?: number): boolean | void {
358358
const oldSessionId = this.getSessionId();
359359

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

370370
// --- There is recent user activity --- //
371371
// 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();
372+
if (!this._loadAndCheckSession(expiry)) {
378373
return;
379374
}
380375

@@ -401,10 +396,10 @@ export class ReplayContainer implements ReplayContainerInterface {
401396
}
402397

403398
/**
404-
* Loads a session from storage, or creates a new one if it does not exist or
405-
* is expired.
399+
* Loads (or refreshes) the current session.
400+
* Returns false if session is not recorded.
406401
*/
407-
private _loadSession({ expiry }: { expiry: number }): void {
402+
private _loadAndCheckSession(expiry = SESSION_IDLE_DURATION): boolean {
408403
const { type, session } = getSession({
409404
expiry,
410405
stickySession: Boolean(this._options.stickySession),
@@ -425,6 +420,13 @@ export class ReplayContainer implements ReplayContainerInterface {
425420
}
426421

427422
this.session = session;
423+
424+
if (!this.session.sampled) {
425+
this.stop();
426+
return false;
427+
}
428+
429+
return true;
428430
}
429431

430432
/**
@@ -630,9 +632,7 @@ export class ReplayContainer implements ReplayContainerInterface {
630632
return;
631633
}
632634

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

637637
if (!isSessionActive) {
638638
// If the user has come back to the page within VISIBILITY_CHANGE_TIMEOUT

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['_loadAndCheckSession'](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['_loadAndCheckSession'](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['_loadAndCheckSession'](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['_loadAndCheckSession'](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['_loadAndCheckSession'](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['_loadAndCheckSession'](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['_loadAndCheckSession'](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['_loadAndCheckSession'](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['_loadAndCheckSession'](SESSION_IDLE_DURATION);
4848
mockRecord.takeFullSnapshot.mockClear();
4949
mockAddInstrumentationHandler.mockClear();
5050
Object.defineProperty(WINDOW, 'location', {

0 commit comments

Comments
 (0)