|
1 | 1 | import {deepEqual, shallowEqual} from 'fast-equals'; |
2 | | -import {useCallback, useEffect, useRef, useSyncExternalStore} from 'react'; |
| 2 | +import {useCallback, useEffect, useMemo, useRef, useSyncExternalStore} from 'react'; |
3 | 3 | import type {DependencyList} from 'react'; |
4 | 4 | import OnyxCache, {TASK} from './OnyxCache'; |
5 | 5 | import type {Connection} from './OnyxConnectionManager'; |
6 | 6 | import connectionManager from './OnyxConnectionManager'; |
7 | 7 | import OnyxUtils from './OnyxUtils'; |
| 8 | +import * as GlobalSettings from './GlobalSettings'; |
8 | 9 | import type {CollectionKeyBase, KeyValueMapping, OnyxCollection, OnyxKey, OnyxValue} from './types'; |
9 | 10 | import useLiveRef from './useLiveRef'; |
10 | 11 | import usePrevious from './usePrevious'; |
| 12 | +import decorateWithMetrics from './metrics'; |
11 | 13 |
|
12 | 14 | type BaseUseOnyxOptions = { |
13 | 15 | /** |
@@ -144,7 +146,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>( |
144 | 146 | // in `getSnapshot()` to be satisfied several times. |
145 | 147 | const isFirstConnectionRef = useRef(true); |
146 | 148 |
|
147 | | - // Indicates if the hook is connecting to a Onyx key. |
| 149 | + // Indicates if the hook is connecting to an Onyx key. |
148 | 150 | const isConnectingRef = useRef(false); |
149 | 151 |
|
150 | 152 | // Stores the `onStoreChange()` function, which can be used to trigger a `getSnapshot()` update when desired. |
@@ -321,11 +323,19 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>( |
321 | 323 | [key, options?.initWithStoredValues, options?.reuseConnection, checkEvictableKey], |
322 | 324 | ); |
323 | 325 |
|
| 326 | + const getSnapshotDecorated = useMemo(() => { |
| 327 | + if (!GlobalSettings.isPerformanceMetricsEnabled()) { |
| 328 | + return getSnapshot; |
| 329 | + } |
| 330 | + |
| 331 | + return decorateWithMetrics(getSnapshot, 'useOnyx.getSnapshot'); |
| 332 | + }, [getSnapshot]); |
| 333 | + |
324 | 334 | useEffect(() => { |
325 | 335 | checkEvictableKey(); |
326 | 336 | }, [checkEvictableKey]); |
327 | 337 |
|
328 | | - const result = useSyncExternalStore<UseOnyxResult<TReturnValue>>(subscribe, getSnapshot); |
| 338 | + const result = useSyncExternalStore<UseOnyxResult<TReturnValue>>(subscribe, getSnapshotDecorated); |
329 | 339 |
|
330 | 340 | return result; |
331 | 341 | } |
|
0 commit comments