Skip to content

Commit 7289d9a

Browse files
committed
ref(replay): Avoid nullish coalescing operator
1 parent 847e16f commit 7289d9a

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

Diff for: packages/replay/src/session/Session.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ export interface Session {
4141
export function makeSession(session: Partial<Session>, { sessionSampleRate, errorSampleRate }: SampleRates): Session {
4242
const now = new Date().getTime();
4343
const id = session.id || uuid4();
44-
const started = session.started ?? now;
45-
const lastActivity = session.lastActivity ?? now;
46-
const segmentId = session.segmentId ?? 0;
47-
const sampled =
48-
session.sampled ?? (isSampled(sessionSampleRate) ? 'session' : isSampled(errorSampleRate) ? 'error' : false);
44+
const started = session.started || now;
45+
const lastActivity = session.lastActivity || now;
46+
const segmentId = session.segmentId || 0;
47+
const sampled = sampleSession(session.sampled, { sessionSampleRate, errorSampleRate });
4948

5049
return {
5150
id,
@@ -55,3 +54,11 @@ export function makeSession(session: Partial<Session>, { sessionSampleRate, erro
5554
sampled,
5655
};
5756
}
57+
58+
function sampleSession(sampled: Sampled | undefined, { sessionSampleRate, errorSampleRate }: SampleRates): Sampled {
59+
if (typeof sampled !== 'undefined') {
60+
return sampled;
61+
}
62+
63+
return isSampled(sessionSampleRate) ? 'session' : isSampled(errorSampleRate) ? 'error' : false;
64+
}

Diff for: packages/replay/test/unit/util/isSessionExpired.test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { isSessionExpired } from '../../../src/util/isSessionExpired';
44
function createSession(extra?: Record<string, any>) {
55
return makeSession(
66
{
7-
started: 0,
8-
lastActivity: 0,
7+
// Setting started/lastActivity to 0 makes it use the default, which is `Date.now()`
8+
started: 1,
9+
lastActivity: 1,
910
segmentId: 0,
1011
...extra,
1112
},
@@ -26,5 +27,5 @@ it('session age is not older than max session life', function () {
2627
});
2728

2829
it('session age is older than max session life', function () {
29-
expect(isSessionExpired(createSession(), 1_800_000, 1_800_000)).toBe(true); // Session expires at ts >= 1_800_000
30+
expect(isSessionExpired(createSession(), 1_800_000, 1_800_001)).toBe(true); // Session expires at ts >= 1_800_000
3031
});

0 commit comments

Comments
 (0)