diff --git a/packages/react-native/src/private/webapis/performance/PerformanceObserver.js b/packages/react-native/src/private/webapis/performance/PerformanceObserver.js index c576a4451f6..e2efde682ea 100644 --- a/packages/react-native/src/private/webapis/performance/PerformanceObserver.js +++ b/packages/react-native/src/private/webapis/performance/PerformanceObserver.js @@ -145,6 +145,22 @@ export class PerformanceObserver { NativePerformance.disconnect(this.#nativeObserverHandle); } + takeRecords(): PerformanceEntryList { + let entries: PerformanceEntryList = []; + + if (this.#nativeObserverHandle != null) { + const rawEntries = NativePerformance.takeRecords( + this.#nativeObserverHandle, + true, + ); + if (rawEntries && rawEntries.length > 0) { + entries = rawEntries.map(rawToPerformanceEntry); + } + } + + return entries; + } + #createNativeObserver(): OpaqueNativeObserverHandle | null { this.#calledAtLeastOnce = false; @@ -154,7 +170,7 @@ export class PerformanceObserver { observerHandle, true, // sort records ); - if (!rawEntries) { + if (!rawEntries || rawEntries.length === 0) { return; } diff --git a/packages/react-native/src/private/webapis/performance/__tests__/PerformanceObserver-itest.js b/packages/react-native/src/private/webapis/performance/__tests__/PerformanceObserver-itest.js index 77af8cb8c83..70e91786f95 100644 --- a/packages/react-native/src/private/webapis/performance/__tests__/PerformanceObserver-itest.js +++ b/packages/react-native/src/private/webapis/performance/__tests__/PerformanceObserver-itest.js @@ -97,4 +97,24 @@ describe('PerformanceObserver', () => { expect(entries1.getEntries()[1]).toBe(measure); expect(entries2.getEntries()[1]).toBe(measure); }); + + describe('takeRecords()', () => { + it('provides all buffered events and clears the buffer', () => { + const callback = jest.fn(); + const observer = new PerformanceObserver(callback); + observer.observe({entryTypes: ['mark']}); + + Fantom.runTask(() => { + const entry = performance.mark('mark1'); + + const entries = observer.takeRecords(); + expect(entries.length).toBe(1); + // This is not supported yet + // expect(entries[0]).toBe(entry); + expect(entries[0]).toEqual(entry); + }); + + expect(callback).not.toHaveBeenCalled(); + }); + }); });