From aef71949961d1f33ef39fc36bda731686bbc5504 Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Mon, 13 Feb 2023 10:44:06 -0800 Subject: [PATCH] API symmetry when passing PerformanceEntryType to/from native module Summary: [Changelog][Internal] `NativePerformanceObserver` TurboModule API would get the type for performance entries as strings in one direction (`start/stopReporting`) and as integers in another direction (inside `RawPerformanceEntry`, for optimization on the native side). This makes is symmetrical and consistent, all the conversions are now handled on the JS side. Reviewed By: christophpurrer Differential Revision: D43236466 fbshipit-source-id: 08e1b62df90e6d26a11577d6b6b1d91a6bce8339 --- .../NativePerformanceObserver.cpp | 21 +++----------- .../NativePerformanceObserver.h | 4 +-- .../NativePerformanceObserver.js | 4 +-- .../WebPerformance/PerformanceObserver.js | 29 +++++++++++++++++-- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/Libraries/WebPerformance/NativePerformanceObserver.cpp b/Libraries/WebPerformance/NativePerformanceObserver.cpp index 8a7aac5c06..dce100dde1 100644 --- a/Libraries/WebPerformance/NativePerformanceObserver.cpp +++ b/Libraries/WebPerformance/NativePerformanceObserver.cpp @@ -10,19 +10,6 @@ namespace facebook::react { -static PerformanceEntryType stringToPerformanceEntryType( - const std::string &entryType) { - if (entryType == "mark") { - return PerformanceEntryType::MARK; - } else if (entryType == "measure") { - return PerformanceEntryType::MEASURE; - } else if (entryType == "event") { - return PerformanceEntryType::EVENT; - } else { - return PerformanceEntryType::UNDEFINED; - } -} - NativePerformanceObserver::NativePerformanceObserver( std::shared_ptr jsInvoker) : NativePerformanceObserverCxxSpec(std::move(jsInvoker)) { @@ -35,16 +22,16 @@ NativePerformanceObserver::~NativePerformanceObserver() { void NativePerformanceObserver::startReporting( jsi::Runtime &rt, - std::string entryType) { + int32_t entryType) { PerformanceEntryReporter::getInstance().startReporting( - stringToPerformanceEntryType(entryType)); + static_cast(entryType)); } void NativePerformanceObserver::stopReporting( jsi::Runtime &rt, - std::string entryType) { + int32_t entryType) { PerformanceEntryReporter::getInstance().stopReporting( - stringToPerformanceEntryType(entryType)); + static_cast(entryType)); } GetPendingEntriesResult NativePerformanceObserver::popPendingEntries( diff --git a/Libraries/WebPerformance/NativePerformanceObserver.h b/Libraries/WebPerformance/NativePerformanceObserver.h index 7eb5349c0e..4dd874f558 100644 --- a/Libraries/WebPerformance/NativePerformanceObserver.h +++ b/Libraries/WebPerformance/NativePerformanceObserver.h @@ -59,9 +59,9 @@ class NativePerformanceObserver NativePerformanceObserver(std::shared_ptr jsInvoker); ~NativePerformanceObserver(); - void startReporting(jsi::Runtime &rt, std::string entryType); + void startReporting(jsi::Runtime &rt, int32_t entryType); - void stopReporting(jsi::Runtime &rt, std::string entryType); + void stopReporting(jsi::Runtime &rt, int32_t entryType); GetPendingEntriesResult popPendingEntries(jsi::Runtime &rt); diff --git a/Libraries/WebPerformance/NativePerformanceObserver.js b/Libraries/WebPerformance/NativePerformanceObserver.js index ae87d47151..b35bfcb3b0 100644 --- a/Libraries/WebPerformance/NativePerformanceObserver.js +++ b/Libraries/WebPerformance/NativePerformanceObserver.js @@ -38,8 +38,8 @@ export type GetPendingEntriesResult = {| |}; export interface Spec extends TurboModule { - +startReporting: (entryType: string) => void; - +stopReporting: (entryType: string) => void; + +startReporting: (entryType: RawPerformanceEntryType) => void; + +stopReporting: (entryType: RawPerformanceEntryType) => void; +popPendingEntries: () => GetPendingEntriesResult; +setOnPerformanceEntryCallback: (callback?: () => void) => void; } diff --git a/Libraries/WebPerformance/PerformanceObserver.js b/Libraries/WebPerformance/PerformanceObserver.js index 088c11eb22..c52313f3af 100644 --- a/Libraries/WebPerformance/PerformanceObserver.js +++ b/Libraries/WebPerformance/PerformanceObserver.js @@ -33,7 +33,26 @@ function rawToPerformanceEntryType( return 'event'; default: throw new TypeError( - `unexpected performance entry type received: ${type}`, + `rawToPerformanceEntryType: unexpected performance entry type received: ${type}`, + ); + } +} + +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}`, ); } } @@ -222,7 +241,9 @@ export default class PerformanceObserver { : requestedEntryTypes; for (const type of newEntryTypes) { if (!observerCountPerEntryType.has(type)) { - NativePerformanceObserver.startReporting(type); + NativePerformanceObserver.startReporting( + performanceEntryTypeToRaw(type), + ); } observerCountPerEntryType.set( type, @@ -248,7 +269,9 @@ export default class PerformanceObserver { observerCountPerEntryType.get(type) ?? 0; if (numberOfObserversForThisType === 1) { observerCountPerEntryType.delete(type); - NativePerformanceObserver.stopReporting(type); + NativePerformanceObserver.stopReporting( + performanceEntryTypeToRaw(type), + ); } else if (numberOfObserversForThisType !== 0) { observerCountPerEntryType.set(type, numberOfObserversForThisType - 1); }