Files
react-native/packages/react-native/Libraries/WebPerformance/EventCounts.js
T
Ruslan Lesiutin 714b502b0c | RN Monorepo | Migrate to package (#36434)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36434

Changelog: [Internal]

This is a squashed stack of 18 commits, starting from D43202126

allow-large-files

Reviewed By: cortinico

Differential Revision: D43977381

fbshipit-source-id: 0da552ddb85f2f61a0be0ef071915b35f3f8555c
2023-03-17 05:03:25 -07:00

79 lines
2.1 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 NativePerformanceObserver from './NativePerformanceObserver';
import {warnNoNativePerformanceObserver} from './PerformanceObserver';
type EventCountsForEachCallbackType =
| (() => void)
| ((value: number) => void)
| ((value: number, key: string) => void)
| ((value: number, key: string, map: Map<string, number>) => void);
let cachedEventCounts: ?Map<string, number>;
function getCachedEventCounts(): Map<string, number> {
if (cachedEventCounts) {
return cachedEventCounts;
}
if (!NativePerformanceObserver) {
warnNoNativePerformanceObserver();
return new Map();
}
cachedEventCounts = new Map<string, number>(
NativePerformanceObserver.getEventCounts(),
);
// $FlowFixMe[incompatible-call]
global.queueMicrotask(() => {
// To be consistent with the calls to the API from the same task,
// but also not to refetch the data from native too often,
// schedule to invalidate the cache later,
// after the current task is guaranteed to have finished.
cachedEventCounts = null;
});
return cachedEventCounts ?? new Map();
}
/**
* Implementation of the EventCounts Web Performance API
* corresponding to the standard in
* https://www.w3.org/TR/event-timing/#eventcounts
*/
export default class EventCounts {
// flowlint unsafe-getters-setters:off
get size(): number {
return getCachedEventCounts().size;
}
entries(): Iterator<[string, number]> {
return getCachedEventCounts().entries();
}
forEach(callback: EventCountsForEachCallbackType): void {
return getCachedEventCounts().forEach(callback);
}
get(key: string): ?number {
return getCachedEventCounts().get(key);
}
has(key: string): boolean {
return getCachedEventCounts().has(key);
}
keys(): Iterator<string> {
return getCachedEventCounts().keys();
}
values(): Iterator<number> {
return getCachedEventCounts().values();
}
}