Files
react-native/Libraries/WebPerformance/RawPerformanceEntry.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

88 lines
2.4 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.
*
* @format
* @flow strict
*/
import type {
RawPerformanceEntry,
RawPerformanceEntryType,
} from './NativePerformanceObserver';
import type {PerformanceEntryType} from './PerformanceEntry';
import {PerformanceEntry} from './PerformanceEntry';
import {PerformanceEventTiming} from './PerformanceEventTiming';
export const RawPerformanceEntryTypeValues = {
UNDEFINED: 0,
MARK: 1,
MEASURE: 2,
EVENT: 3,
};
export function rawToPerformanceEntry(
entry: RawPerformanceEntry,
): PerformanceEntry {
if (entry.entryType === RawPerformanceEntryTypeValues.EVENT) {
return new PerformanceEventTiming({
name: entry.name,
startTime: entry.startTime,
duration: entry.duration,
processingStart: entry.processingStart,
processingEnd: entry.processingEnd,
interactionId: entry.interactionId,
});
} else {
return new PerformanceEntry({
name: entry.name,
entryType: rawToPerformanceEntryType(entry.entryType),
startTime: entry.startTime,
duration: entry.duration,
});
}
}
export function rawToPerformanceEntryType(
type: RawPerformanceEntryType,
): PerformanceEntryType {
switch (type) {
case RawPerformanceEntryTypeValues.MARK:
return 'mark';
case RawPerformanceEntryTypeValues.MEASURE:
return 'measure';
case RawPerformanceEntryTypeValues.EVENT:
return 'event';
case RawPerformanceEntryTypeValues.UNDEFINED:
throw new TypeError(
"rawToPerformanceEntryType: UNDEFINED can't be cast to PerformanceEntryType",
);
default:
throw new TypeError(
`rawToPerformanceEntryType: unexpected performance entry type received: ${type}`,
);
}
}
export function performanceEntryTypeToRaw(
type: PerformanceEntryType,
): RawPerformanceEntryType {
switch (type) {
case 'mark':
return RawPerformanceEntryTypeValues.MARK;
case 'measure':
return RawPerformanceEntryTypeValues.MEASURE;
case 'event':
return RawPerformanceEntryTypeValues.EVENT;
default:
// Verify exhaustive check with Flow
(type: empty);
throw new TypeError(
`performanceEntryTypeToRaw: unexpected performance entry type received: ${type}`,
);
}
}