Skip to content

Commit 81f8b0a

Browse files
rubennortefacebook-github-bot
authored andcommitted
Implement PerformanceObserver.takeRecords() (#53428)
Summary: Pull Request resolved: #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
1 parent 8ed0fa8 commit 81f8b0a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

packages/react-native/src/private/webapis/performance/PerformanceObserver.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,22 @@ export class PerformanceObserver {
145145
NativePerformance.disconnect(this.#nativeObserverHandle);
146146
}
147147

148+
takeRecords(): PerformanceEntryList {
149+
let entries: PerformanceEntryList = [];
150+
151+
if (this.#nativeObserverHandle != null) {
152+
const rawEntries = NativePerformance.takeRecords(
153+
this.#nativeObserverHandle,
154+
true,
155+
);
156+
if (rawEntries && rawEntries.length > 0) {
157+
entries = rawEntries.map(rawToPerformanceEntry);
158+
}
159+
}
160+
161+
return entries;
162+
}
163+
148164
#createNativeObserver(): OpaqueNativeObserverHandle | null {
149165
this.#calledAtLeastOnce = false;
150166

@@ -154,7 +170,7 @@ export class PerformanceObserver {
154170
observerHandle,
155171
true, // sort records
156172
);
157-
if (!rawEntries) {
173+
if (!rawEntries || rawEntries.length === 0) {
158174
return;
159175
}
160176

packages/react-native/src/private/webapis/performance/__tests__/PerformanceObserver-itest.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,24 @@ describe('PerformanceObserver', () => {
9797
expect(entries1.getEntries()[1]).toBe(measure);
9898
expect(entries2.getEntries()[1]).toBe(measure);
9999
});
100+
101+
describe('takeRecords()', () => {
102+
it('provides all buffered events and clears the buffer', () => {
103+
const callback = jest.fn();
104+
const observer = new PerformanceObserver(callback);
105+
observer.observe({entryTypes: ['mark']});
106+
107+
Fantom.runTask(() => {
108+
const entry = performance.mark('mark1');
109+
110+
const entries = observer.takeRecords();
111+
expect(entries.length).toBe(1);
112+
// This is not supported yet
113+
// expect(entries[0]).toBe(entry);
114+
expect(entries[0]).toEqual(entry);
115+
});
116+
117+
expect(callback).not.toHaveBeenCalled();
118+
});
119+
});
100120
});

0 commit comments

Comments
 (0)