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 {createResource} from 'react-devtools-shared/src/devtools/cache';
    
  11. import {importFile} from './import-worker';
    
  12. 
    
  13. import type {Resource} from 'react-devtools-shared/src/devtools/cache';
    
  14. import type {TimelineData} from './types';
    
  15. import type {ImportWorkerOutputData} from './import-worker/index';
    
  16. 
    
  17. export type DataResource = Resource<void, File, TimelineData | Error>;
    
  18. 
    
  19. export default function createDataResourceFromImportedFile(
    
  20.   file: File,
    
  21. ): DataResource {
    
  22.   return createResource(
    
  23.     () => {
    
  24.       return new Promise<TimelineData | Error>((resolve, reject) => {
    
  25.         const promise = ((importFile(
    
  26.           file,
    
  27.         ): any): Promise<ImportWorkerOutputData>);
    
  28.         promise.then(data => {
    
  29.           switch (data.status) {
    
  30.             case 'SUCCESS':
    
  31.               resolve(data.processedData);
    
  32.               break;
    
  33.             case 'INVALID_PROFILE_ERROR':
    
  34.               resolve(data.error);
    
  35.               break;
    
  36.             case 'UNEXPECTED_ERROR':
    
  37.               reject(data.error);
    
  38.               break;
    
  39.           }
    
  40.         });
    
  41.       });
    
  42.     },
    
  43.     () => file,
    
  44.     {useWeakMap: true},
    
  45.   );
    
  46. }