Skip to content

Commit fcb805a

Browse files
committed
ref: Move check to _loadAndCheckSession
1 parent 5e312dc commit fcb805a

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
@@ -155,7 +155,9 @@ export class ReplayContainer implements ReplayContainerInterface {
155155
public start(): void {
156156
this._setInitialState();
157157

158-
this._loadSession({ expiry: SESSION_IDLE_DURATION });
158+
if (!this._loadAndCheckSession()) {
159+
return;
160+
}
159161

160162
// If there is no session, then something bad has happened - can't continue
161163
if (!this.session) {
@@ -234,6 +236,10 @@ export class ReplayContainer implements ReplayContainerInterface {
234236
* does not support a teardown
235237
*/
236238
public stop(): void {
239+
if (!this._isEnabled) {
240+
return;
241+
}
242+
237243
try {
238244
__DEBUG_BUILD__ && logger.log('[Replay] Stopping Replays');
239245
this._isEnabled = false;
@@ -264,8 +270,7 @@ export class ReplayContainer implements ReplayContainerInterface {
264270
* new DOM checkout.`
265271
*/
266272
public resume(): void {
267-
if (!this.session || !this.session.sampled) {
268-
this.stop();
273+
if (!this._loadAndCheckSession()) {
269274
return;
270275
}
271276

@@ -315,12 +320,7 @@ export class ReplayContainer implements ReplayContainerInterface {
315320
if (!this._stopRecording) {
316321
// Create a new session, otherwise when the user action is flushed, it
317322
// will get rejected due to an expired session.
318-
this._loadSession({ expiry: SESSION_IDLE_DURATION });
319-
320-
// We know this is set, because it is always set in _loadSession
321-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
322-
if (!this.session!.sampled) {
323-
this.stop();
323+
if (!this._loadAndCheckSession()) {
324324
return;
325325
}
326326

@@ -360,7 +360,7 @@ export class ReplayContainer implements ReplayContainerInterface {
360360
* Returns true if session is not expired, false otherwise.
361361
* @hidden
362362
*/
363-
public checkAndHandleExpiredSession({ expiry = SESSION_IDLE_DURATION }: { expiry?: number } = {}): boolean | void {
363+
public checkAndHandleExpiredSession(expiry?: number): boolean | void {
364364
const oldSessionId = this.getSessionId();
365365

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

376376
// --- There is recent user activity --- //
377377
// This will create a new session if expired, based on expiry length
378-
this._loadSession({ expiry });
379-
380-
// We know this is set, because it is always set in _loadSession
381-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
382-
if (!this.session!.sampled) {
383-
this.stop();
378+
if (!this._loadAndCheckSession(expiry)) {
384379
return;
385380
}
386381

@@ -407,10 +402,10 @@ export class ReplayContainer implements ReplayContainerInterface {
407402
}
408403

409404
/**
410-
* Loads a session from storage, or creates a new one if it does not exist or
411-
* is expired.
405+
* Loads (or refreshes) the current session.
406+
* Returns false if session is not recorded.
412407
*/
413-
private _loadSession({ expiry }: { expiry: number }): void {
408+
private _loadAndCheckSession(expiry = SESSION_IDLE_DURATION): boolean {
414409
const { type, session } = getSession({
415410
expiry,
416411
stickySession: Boolean(this._options.stickySession),
@@ -431,6 +426,13 @@ export class ReplayContainer implements ReplayContainerInterface {
431426
}
432427

433428
this.session = session;
429+
430+
if (!this.session.sampled) {
431+
this.stop();
432+
return false;
433+
}
434+
435+
return true;
434436
}
435437

436438
/**
@@ -637,9 +639,7 @@ export class ReplayContainer implements ReplayContainerInterface {
637639
return;
638640
}
639641

640-
const isSessionActive = this.checkAndHandleExpiredSession({
641-
expiry: VISIBILITY_CHANGE_TIMEOUT,
642-
});
642+
const isSessionActive = this.checkAndHandleExpiredSession(VISIBILITY_CHANGE_TIMEOUT);
643643

644644
if (!isSessionActive) {
645645
// 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)