Implement PerformanceObserver.takeRecords() (#53428)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/53428

Changelog: [internal]

This is the last method in `PerformanceObserver` to implement. For some reason we never added it, even though it was trivial.

Reviewed By: rshest

Differential Revision: D80717237

fbshipit-source-id: ae3bd243d0f3f0fe4f0705437d78d14c532515f7
This commit is contained in:
Rubén Norte
2025-09-08 14:43:50 +01:00
committed by Vitali Zaidman
parent 1cacdf70a1
commit 68e774bb2c
2 changed files with 37 additions and 1 deletions
@@ -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;
}
@@ -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();
});
});
});