Skip to content

Commit c2da175

Browse files
OskarEichlermeta-codesync[bot]
authored andcommitted
Validate PerformanceObserver callbacks (#58239)
Summary: `PerformanceObserver` currently accepts missing and non-function callbacks, creating invalid instances that fail only when native delivery tries to invoke the stored value. Validate construction synchronously, distinguish an omitted required argument from an explicitly supplied non-function, and cover both error paths. Fixes #58238. ## Changelog: [GENERAL] [FIXED] - Reject missing and non-function PerformanceObserver callbacks during construction. Pull Request resolved: #58239 Test Plan: - Exact upstream focused regression: both new constructor assertions failed. - Fixed Fantom suite: 5 passed with one pre-existing skip. - Fresh Flow check: 0 errors. - Targeted no-ignore ESLint, Prettier, and `git diff --check` passed. Invalid constructor calls now throw synchronously as required; valid callers and UI behavior are unchanged. Reviewed By: christophpurrer Differential Revision: D123889539 Pulled By: rubennorte fbshipit-source-id: 8ee2f483bfa3aa3bc764efb7c8505ec7f69a349b
1 parent 99aefad commit c2da175

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ export class PerformanceObserver {
121121
#calledAtLeastOnce = false;
122122

123123
constructor(callback: PerformanceObserverCallback) {
124+
if (arguments.length === 0) {
125+
throw new TypeError(
126+
"Failed to construct 'PerformanceObserver': 1 argument required, but only 0 present.",
127+
);
128+
}
129+
130+
if (typeof callback !== 'function') {
131+
throw new TypeError(
132+
"Failed to construct 'PerformanceObserver': parameter 1 is not of type 'Function'.",
133+
);
134+
}
135+
124136
this.#callback = callback;
125137
}
126138

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@ import {PerformanceObserverEntryList_public} from '../PerformanceObserver';
1414
import * as Fantom from '@react-native/fantom';
1515

1616
describe('PerformanceObserver', () => {
17+
describe('constructor', () => {
18+
it('requires a callback', () => {
19+
expect(() => {
20+
// $FlowExpectedError[incompatible-type]
21+
return new PerformanceObserver();
22+
}).toThrow(
23+
"Failed to construct 'PerformanceObserver': 1 argument required, but only 0 present.",
24+
);
25+
});
26+
27+
it('requires the callback to be a function', () => {
28+
expect(() => {
29+
// $FlowExpectedError[incompatible-type]
30+
return new PerformanceObserver(null);
31+
}).toThrow(
32+
"Failed to construct 'PerformanceObserver': parameter 1 is not of type 'Function'.",
33+
);
34+
expect(() => {
35+
// $FlowExpectedError[incompatible-type]
36+
return new PerformanceObserver('not a function');
37+
}).toThrow(
38+
"Failed to construct 'PerformanceObserver': parameter 1 is not of type 'Function'.",
39+
);
40+
});
41+
});
42+
1743
it('receives notifications for marks and measures', () => {
1844
const callback = jest.fn();
1945
const observer = new PerformanceObserver(callback);

0 commit comments

Comments
 (0)