Skip to content

Commit 53a9f7b

Browse files
committed
ref(replay): Extract addMemoryEntry out
1 parent 322ba2d commit 53a9f7b

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

packages/replay/src/replay.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { handleGlobalEventListener } from './coreHandlers/handleGlobalEvent';
1919
import { handleHistorySpanListener } from './coreHandlers/handleHistory';
2020
import { handleXhrSpanListener } from './coreHandlers/handleXhr';
2121
import { setupPerformanceObserver } from './coreHandlers/performanceObserver';
22-
import { createMemoryEntry, createPerformanceEntries } from './createPerformanceEntry';
22+
import { createPerformanceEntries } from './createPerformanceEntry';
2323
import { createEventBuffer, EventBuffer } from './eventBuffer';
2424
import { deleteSession } from './session/deleteSession';
2525
import { getSession } from './session/getSession';
@@ -36,6 +36,7 @@ import type {
3636
SendReplay,
3737
} from './types';
3838
import { addEvent } from './util/addEvent';
39+
import { addMemoryEntry } from './util/addMemoryEntry';
3940
import { captureInternalException } from './util/captureInternalException';
4041
import { createBreadcrumb } from './util/createBreadcrumb';
4142
import { createPayload } from './util/createPayload';
@@ -701,23 +702,6 @@ export class ReplayContainer {
701702
return createPerformanceSpans(this, createPerformanceEntries(entries));
702703
}
703704

704-
/**
705-
* Create a "span" for the total amount of memory being used by JS objects
706-
* (including v8 internal objects).
707-
*/
708-
addMemoryEntry(): Promise<void[]> | undefined {
709-
// window.performance.memory is a non-standard API and doesn't work on all browsers
710-
// so we check before creating the event.
711-
if (!('memory' in WINDOW.performance)) {
712-
return;
713-
}
714-
715-
return createPerformanceSpans(this, [
716-
// @ts-ignore memory doesn't exist on type Performance as the API is non-standard (we check that it exists above)
717-
createMemoryEntry(WINDOW.performance.memory),
718-
]);
719-
}
720-
721705
/**
722706
* Checks if recording should be stopped due to user inactivity. Otherwise
723707
* check if session is expired and create a new session if so. Triggers a new
@@ -819,7 +803,7 @@ export class ReplayContainer {
819803
}
820804

821805
// Only attach memory event if eventBuffer is not empty
822-
await this.addMemoryEntry();
806+
await addMemoryEntry(this);
823807

824808
try {
825809
// Note this empties the event buffer regardless of outcome of sending replay
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { WINDOW } from '../constants';
2+
import { ReplayContainer } from '../replay';
3+
import { createPerformanceSpans } from './createPerformanceSpans';
4+
5+
/**
6+
* Create a "span" for the total amount of memory being used by JS objects
7+
* (including v8 internal objects).
8+
*/
9+
export function addMemoryEntry(replay: ReplayContainer): Promise<void[]> | undefined {
10+
// window.performance.memory is a non-standard API and doesn't work on all browsers
11+
// so we check before creating the event.
12+
if (!('memory' in WINDOW.performance)) {
13+
return;
14+
}
15+
16+
return createPerformanceSpans(replay, [
17+
// @ts-ignore memory doesn't exist on type Performance as the API is non-standard (we check that it exists above)
18+
createMemoryEntry(WINDOW.performance.memory),
19+
]);
20+
}

packages/replay/test/unit/flush.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as SentryUtils from '@sentry/utils';
22

33
import { SESSION_IDLE_DURATION, WINDOW } from '../../src/constants';
4+
import * as AddMemoryEntry from '../../src/util/addMemoryEntry';
45
import { createPerformanceSpans } from '../../src/util/createPerformanceSpans';
56
import { createPerformanceEntries } from './../../src/createPerformanceEntry';
67
import { ReplayContainer } from './../../src/replay';
@@ -16,7 +17,7 @@ async function advanceTimers(time: number) {
1617

1718
type MockSendReplay = jest.MockedFunction<typeof ReplayContainer.prototype.sendReplay>;
1819
type MockAddPerformanceEntries = jest.MockedFunction<typeof ReplayContainer.prototype.addPerformanceEntries>;
19-
type MockAddMemoryEntry = jest.MockedFunction<typeof ReplayContainer.prototype.addMemoryEntry>;
20+
type MockAddMemoryEntry = jest.SpyInstance;
2021
type MockEventBufferFinish = jest.MockedFunction<Exclude<typeof ReplayContainer.prototype.eventBuffer, null>['finish']>;
2122
type MockFlush = jest.MockedFunction<typeof ReplayContainer.prototype.flush>;
2223
type MockRunFlush = jest.MockedFunction<typeof ReplayContainer.prototype.runFlush>;
@@ -63,8 +64,7 @@ beforeAll(async () => {
6364
return [];
6465
});
6566

66-
jest.spyOn(replay, 'addMemoryEntry');
67-
mockAddMemoryEntry = replay.addMemoryEntry as MockAddMemoryEntry;
67+
mockAddMemoryEntry = jest.spyOn(AddMemoryEntry, 'addMemoryEntry');
6868
});
6969

7070
beforeEach(() => {

0 commit comments

Comments
 (0)