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. /**
    
  11.  * This is a renderer of React that doesn't have a render target output.
    
  12.  * It is useful to demonstrate the internals of the reconciler in isolation
    
  13.  * and for testing semantics of reconciliation separate from the host
    
  14.  * environment.
    
  15.  */
    
  16. 
    
  17. import {readModule} from 'react-noop-renderer/flight-modules';
    
  18. 
    
  19. import ReactFlightClient from 'react-client/flight';
    
  20. 
    
  21. type Source = Array<Uint8Array>;
    
  22. 
    
  23. const decoderOptions = {stream: true};
    
  24. 
    
  25. const {createResponse, processBinaryChunk, getRoot, close} = ReactFlightClient({
    
  26.   createStringDecoder() {
    
  27.     return new TextDecoder();
    
  28.   },
    
  29.   readPartialStringChunk(decoder: TextDecoder, buffer: Uint8Array): string {
    
  30.     return decoder.decode(buffer, decoderOptions);
    
  31.   },
    
  32.   readFinalStringChunk(decoder: TextDecoder, buffer: Uint8Array): string {
    
  33.     return decoder.decode(buffer);
    
  34.   },
    
  35.   resolveClientReference(bundlerConfig: null, idx: string) {
    
  36.     return idx;
    
  37.   },
    
  38.   prepareDestinationForModule(moduleLoading: null, metadata: string) {},
    
  39.   preloadModule(idx: string) {},
    
  40.   requireModule(idx: string) {
    
  41.     return readModule(idx);
    
  42.   },
    
  43.   parseModel(response: Response, json) {
    
  44.     return JSON.parse(json, response._fromJSON);
    
  45.   },
    
  46. });
    
  47. 
    
  48. function read<T>(source: Source): Thenable<T> {
    
  49.   const response = createResponse(source, null);
    
  50.   for (let i = 0; i < source.length; i++) {
    
  51.     processBinaryChunk(response, source[i], 0);
    
  52.   }
    
  53.   close(response);
    
  54.   return getRoot(response);
    
  55. }
    
  56. 
    
  57. export {read};