main
js 45 lines 1.56 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 import {disableClientCache} from 'shared/ReactFeatureFlags';
11 import {
12 cache as cacheImpl,
13 cacheSignal as cacheSignalImpl,
14 } from './ReactCacheImpl';
15
16 function noopCache<A: Iterable<mixed>, T>(fn: (...A) => T): (...A) => T {
17 // On the client (i.e. not a Server Components environment) `cache` has
18 // no caching behavior. We just return the function as-is.
19 //
20 // We intend to implement client caching in a future major release. In the
21 // meantime, it's only exposed as an API so that Shared Components can use
22 // per-request caching on the server without breaking on the client. But it
23 // does mean they need to be aware of the behavioral difference.
24 //
25 // The rest of the behavior is the same as the server implementation — it
26 // returns a new reference, extra properties like `displayName` are not
27 // preserved, the length of the new function is 0, etc. That way apps can't
28 // accidentally depend on those details.
29 return function () {
30 // $FlowFixMe[incompatible-type]: We don't want to use rest arguments since we transpile the code.
31 return fn.apply(null, arguments);
32 };
33 }
34
35 export const cache: typeof noopCache = disableClientCache
36 ? noopCache
37 : cacheImpl;
38
39 function noopCacheSignal(): null | AbortSignal {
40 return null;
41 }
42
43 export const cacheSignal: () => null | AbortSignal = disableClientCache
44 ? noopCacheSignal
45 : cacheSignalImpl;