-
Notifications
You must be signed in to change notification settings - Fork 45.6k
/
ReactNoopFlightServer.js
62 lines (54 loc) · 1.82 KB
/
ReactNoopFlightServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
/**
* This is a renderer of React that doesn't have a render target output.
* It is useful to demonstrate the internals of the reconciler in isolation
* and for testing semantics of reconciliation separate from the host
* environment.
*/
import type {ReactModel} from 'react-server/src/ReactFlightServer';
import {saveModule} from 'react-noop-renderer/flight-modules';
import ReactFlightServer from 'react-server/flight';
type Destination = Array<string>;
const ReactNoopFlightServer = ReactFlightServer({
scheduleWork(callback: () => void) {
callback();
},
beginWriting(destination: Destination): void {},
writeChunk(destination: Destination, buffer: Uint8Array): void {
destination.push(Buffer.from((buffer: any)).toString('utf8'));
},
completeWriting(destination: Destination): void {},
close(destination: Destination): void {},
flushBuffered(destination: Destination): void {},
convertStringToBuffer(content: string): Uint8Array {
return Buffer.from(content, 'utf8');
},
formatChunkAsString(type: string, props: Object): string {
return JSON.stringify({type, props});
},
formatChunk(type: string, props: Object): Uint8Array {
return Buffer.from(JSON.stringify({type, props}), 'utf8');
},
resolveModuleMetaData(config: void, renderFn: Function) {
return saveModule(renderFn);
},
});
function render(model: ReactModel): Destination {
let destination: Destination = [];
let bundlerConfig = undefined;
let request = ReactNoopFlightServer.createRequest(
model,
destination,
bundlerConfig,
);
ReactNoopFlightServer.startWork(request);
return destination;
}
export {render};