Skip to content

Commit d9f3f54

Browse files
committed
fix: validate PerformanceObserver observe options
1 parent ef683f7 commit d9f3f54

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,30 @@ export class PerformanceObserver {
203203
return observerHandle;
204204
}
205205

206-
#validateObserveOptions(options: PerformanceObserverInit): void {
207-
const {type, entryTypes, durationThreshold} = options;
206+
#validateObserveOptions(options: ?PerformanceObserverInit): void {
207+
if (options == null) {
208+
throw new TypeError(
209+
"Failed to execute 'observe' on 'PerformanceObserver': 1 argument required, but only 0 present.",
210+
);
211+
}
212+
213+
const {type, entryTypes, durationThreshold, buffered} = options;
208214

209215
if (!type && !entryTypes) {
210216
throw new TypeError(
211-
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must not include both entryTypes and type arguments.",
217+
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must include either entryTypes or type arguments.",
212218
);
213219
}
214220

215221
if (entryTypes && type) {
216222
throw new TypeError(
217-
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must include either entryTypes or type arguments.",
223+
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must not include both entryTypes and type arguments.",
224+
);
225+
}
226+
227+
if (entryTypes && buffered != null) {
228+
throw new TypeError(
229+
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must not include both entryTypes and buffered arguments.",
218230
);
219231
}
220232

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,31 @@ describe('PerformanceObserver', () => {
9696
expect(entries2.getEntries()[1]).toBe(measure);
9797
});
9898

99+
describe('observe()', () => {
100+
it('rejects invalid entry type option combinations before registering with native', () => {
101+
const callback = jest.fn();
102+
const observer = new PerformanceObserver(callback);
103+
104+
// $FlowExpectedError[incompatible-call]
105+
expect(() => observer.observe()).toThrow(
106+
"Failed to execute 'observe' on 'PerformanceObserver': 1 argument required, but only 0 present.",
107+
);
108+
expect(() => observer.observe({})).toThrow(
109+
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must include either entryTypes or type arguments.",
110+
);
111+
expect(() =>
112+
observer.observe({entryTypes: ['mark'], type: 'measure'}),
113+
).toThrow(
114+
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must not include both entryTypes and type arguments.",
115+
);
116+
expect(() =>
117+
observer.observe({entryTypes: ['mark'], buffered: true}),
118+
).toThrow(
119+
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must not include both entryTypes and buffered arguments.",
120+
);
121+
});
122+
});
123+
99124
describe('takeRecords()', () => {
100125
it('provides all buffered events and clears the buffer', () => {
101126
const callback = jest.fn();

0 commit comments

Comments
 (0)