From 3bfff95c6df343d8fa73be3672fd92222d1b6772 Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Mon, 9 Dec 2024 18:10:45 +0100 Subject: [PATCH 1/2] feat: decorate useOnyx.getSnapshot with performance metrics --- lib/useOnyx.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/useOnyx.ts b/lib/useOnyx.ts index 701e3ac4..4a0860d3 100644 --- a/lib/useOnyx.ts +++ b/lib/useOnyx.ts @@ -1,13 +1,15 @@ import {deepEqual, shallowEqual} from 'fast-equals'; -import {useCallback, useEffect, useRef, useSyncExternalStore} from 'react'; +import {useCallback, useEffect, useMemo, useRef, useSyncExternalStore} from 'react'; import type {DependencyList} from 'react'; import OnyxCache, {TASK} from './OnyxCache'; import type {Connection} from './OnyxConnectionManager'; import connectionManager from './OnyxConnectionManager'; import OnyxUtils from './OnyxUtils'; +import * as GlobalSettings from './GlobalSettings'; import type {CollectionKeyBase, KeyValueMapping, OnyxCollection, OnyxKey, OnyxValue} from './types'; import useLiveRef from './useLiveRef'; import usePrevious from './usePrevious'; +import decorateWithMetrics from './metrics'; type BaseUseOnyxOptions = { /** @@ -321,11 +323,19 @@ function useOnyx>( [key, options?.initWithStoredValues, options?.reuseConnection, checkEvictableKey], ); + const getSnapshotDecorated = useMemo(() => { + if (!GlobalSettings.isPerformanceMetricsEnabled()) { + return getSnapshot; + } + + return decorateWithMetrics(getSnapshot, 'useOnyx.getSnapshot'); + }, [getSnapshot]); + useEffect(() => { checkEvictableKey(); }, [checkEvictableKey]); - const result = useSyncExternalStore>(subscribe, getSnapshot); + const result = useSyncExternalStore>(subscribe, getSnapshotDecorated); return result; } From 8205201e035bd9268438ed2f09c448a8bdfc489d Mon Sep 17 00:00:00 2001 From: Adam Horodyski Date: Tue, 10 Dec 2024 15:22:13 +0100 Subject: [PATCH 2/2] fix: remove the in-memory set for decorated aliases to allow react integration --- lib/metrics.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/metrics.ts b/lib/metrics.ts index 301a5467..5ae0c0cd 100644 --- a/lib/metrics.ts +++ b/lib/metrics.ts @@ -1,7 +1,5 @@ import PerformanceProxy from './dependencies/PerformanceProxy'; -const decoratedAliases = new Set(); - /** * Capture a measurement between the start mark and now */ @@ -21,11 +19,6 @@ function isPromiseLike(value: unknown): value is Promise { * Wraps a function with metrics capturing logic */ function decorateWithMetrics(func: (...args: Args) => ReturnType, alias = func.name) { - if (decoratedAliases.has(alias)) { - throw new Error(`"${alias}" is already decorated`); - } - - decoratedAliases.add(alias); function decorated(...args: Args) { const mark = PerformanceProxy.mark(alias, {detail: {args, alias}});