-
Notifications
You must be signed in to change notification settings - Fork 51.1k
Expand file tree
/
Copy pathReactNoopFlightClient.js
More file actions
113 lines (102 loc) · 3.23 KB
/
ReactNoopFlightClient.js
File metadata and controls
113 lines (102 loc) · 3.23 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Copyright (c) Meta Platforms, Inc. and 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 {FindSourceMapURLCallback} from 'react-client/flight';
import {readModule} from 'react-noop-renderer/flight-modules';
import ReactFlightClient from 'react-client/flight';
type Source = Array<Uint8Array>;
const decoderOptions = {stream: true};
const {createResponse, createStreamState, processBinaryChunk, getRoot, close} =
ReactFlightClient({
createStringDecoder() {
return new TextDecoder();
},
readPartialStringChunk(decoder: TextDecoder, buffer: Uint8Array): string {
return decoder.decode(buffer, decoderOptions);
},
readFinalStringChunk(decoder: TextDecoder, buffer: Uint8Array): string {
return decoder.decode(buffer);
},
resolveClientReference(bundlerConfig: null, idx: string) {
return idx;
},
prepareDestinationForModule(moduleLoading: null, metadata: string) {},
preloadModule(idx: string) {},
requireModule(idx: string) {
return readModule(idx);
},
bindToConsole(methodName, args, badgeName) {
return Function.prototype.bind.apply(
// eslint-disable-next-line react-internal/no-production-logging
console[methodName],
[console].concat(args),
);
},
checkEvalAvailabilityOnceDev,
});
type ReadOptions = {|
findSourceMapURL?: FindSourceMapURLCallback,
debugChannel?: {onMessage: (message: string) => void},
close?: boolean,
|};
function read<T>(source: Source, options: ReadOptions): Thenable<T> {
const response = createResponse(
source,
null,
null,
undefined,
undefined,
undefined,
undefined,
false,
options !== undefined ? options.findSourceMapURL : undefined,
true,
undefined,
__DEV__ && options !== undefined && options.debugChannel !== undefined
? options.debugChannel.onMessage
: undefined,
);
const streamState = createStreamState(response, source);
for (let i = 0; i < source.length; i++) {
processBinaryChunk(response, streamState, source[i], 0);
}
if (options !== undefined && options.close) {
close(response);
}
return getRoot(response);
}
let hasConfirmedEval = false;
function checkEvalAvailabilityOnceDev(): void {
if (__DEV__) {
if (!hasConfirmedEval) {
hasConfirmedEval = true;
try {
// eslint-disable-next-line no-eval
(0, eval)('null');
} catch {
console.error(
'eval() is not supported in this environment. ' +
'React requires eval() in development mode for various debugging features ' +
'like reconstructing callstacks from a different environment.\n' +
'React will never use eval() in production mode',
);
}
}
} else {
throw new Error(
'checkEvalAvailabilityOnceDev should never be called in production mode. This is a bug in React.',
);
}
}
export {read};