1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and 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 type {CacheDispatcher} from 'react-reconciler/src/ReactInternalTypes';
    
  11. 
    
  12. import {resolveRequest, getCache} from '../ReactFlightServer';
    
  13. 
    
  14. function createSignal(): AbortSignal {
    
  15.   return new AbortController().signal;
    
  16. }
    
  17. 
    
  18. function resolveCache(): Map<Function, mixed> {
    
  19.   const request = resolveRequest();
    
  20.   if (request) {
    
  21.     return getCache(request);
    
  22.   }
    
  23.   return new Map();
    
  24. }
    
  25. 
    
  26. export const DefaultCacheDispatcher: CacheDispatcher = {
    
  27.   getCacheSignal(): AbortSignal {
    
  28.     const cache = resolveCache();
    
  29.     let entry: AbortSignal | void = (cache.get(createSignal): any);
    
  30.     if (entry === undefined) {
    
  31.       entry = createSignal();
    
  32.       cache.set(createSignal, entry);
    
  33.     }
    
  34.     return entry;
    
  35.   },
    
  36.   getCacheForType<T>(resourceType: () => T): T {
    
  37.     const cache = resolveCache();
    
  38.     let entry: T | void = (cache.get(resourceType): any);
    
  39.     if (entry === undefined) {
    
  40.       entry = resourceType();
    
  41.       // TODO: Warn if undefined?
    
  42.       cache.set(resourceType, entry);
    
  43.     }
    
  44.     return entry;
    
  45.   },
    
  46. };