Files
react-native/Libraries/WebPerformance/__mocks__/NativePerformanceObserver.js
T
Ruslan Shestopalyuk f76d4dee6f Reference implementation (mock) for NativePerformanceObserver (#36116)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36116

[Changelog][Internal]

Add a minimal/reference JavaScript implementation for NativePerformanceObserver - the purpose is both unit testing (JS and native sides separately) and potentially shimming the part of functionality that is not dependent on native side.

This is both a setup for adding general unit tests for the Performance* APIs, but also to be able to do non-trivial changes on JS side for WebPerformance (such as in (D43154319).

Reviewed By: rubennorte

Differential Revision: D43167392

fbshipit-source-id: 213d9534d810dece1dd464f910e92e08dbf39508
2023-02-15 06:03:12 -08:00

57 lines
1.5 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/
import type {
GetPendingEntriesResult,
RawPerformanceEntry,
RawPerformanceEntryType,
Spec as NativePerformanceObserver,
} from '../NativePerformanceObserver';
const reportingType: Set<RawPerformanceEntryType> = new Set();
let entries: Array<RawPerformanceEntry> = [];
let onPerformanceEntryCallback: ?() => void;
const NativePerformanceObserverMock: NativePerformanceObserver = {
startReporting: (entryType: RawPerformanceEntryType) => {
reportingType.add(entryType);
},
stopReporting: (entryType: RawPerformanceEntryType) => {
reportingType.delete(entryType);
},
popPendingEntries: (): GetPendingEntriesResult => {
const res = entries;
entries = [];
return {
droppedEntriesCount: 0,
entries: res,
};
},
setOnPerformanceEntryCallback: (callback?: () => void) => {
onPerformanceEntryCallback = callback;
},
logRawEntry: (entry: RawPerformanceEntry) => {
if (reportingType.has(entry.entryType)) {
entries.push(entry);
// $FlowFixMe[incompatible-call]
global.queueMicrotask(() => {
// We want to emulate the way it's done in native (i.e. async/batched)
onPerformanceEntryCallback?.();
});
}
},
};
export default NativePerformanceObserverMock;