Skip to content

Commit c052b30

Browse files
committed
ref(replay): Extract addMemoryEntry out
1 parent 232e285 commit c052b30

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';
@@ -699,23 +700,6 @@ export class ReplayContainer {
699700
return createPerformanceSpans(this, createPerformanceEntries(entries));
700701
}
701702

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

819803
// Only attach memory event if eventBuffer is not empty
820-
await this.addMemoryEntry();
804+
await addMemoryEntry(this);
821805

822806
try {
823807
// 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)