32
bootstrapModules?: Array<string>,
33
progressiveChunkSize?: number,
34
signal?: AbortSignal,
35
- onCompleteShell?: () => void,
35
onCompleteAll?: () => void,
36
onError?: (error: mixed) => void,
37
|};
39
function renderToReadableStream(
40
children: ReactNodeList,
41
options?: Options,
43
-): ReadableStream {
44
- const request = createRequest(
45
- children,
46
- createResponseState(
47
- options ? options.identifierPrefix : undefined,
48
- options ? options.nonce : undefined,
49
- options ? options.bootstrapScriptContent : undefined,
50
- options ? options.bootstrapScripts : undefined,
51
- options ? options.bootstrapModules : undefined,
52
- ),
53
- createRootFormatContext(options ? options.namespaceURI : undefined),
54
- options ? options.progressiveChunkSize : undefined,
55
- options ? options.onError : undefined,
56
- options ? options.onCompleteAll : undefined,
57
- options ? options.onCompleteShell : undefined,
58
- );
59
- if (options && options.signal) {
60
- const signal = options.signal;
61
- const listener = () => {
62
- abort(request);
63
- signal.removeEventListener('abort', listener);
64
- };
65
- signal.addEventListener('abort', listener);
66
- }
67
- const stream = new ReadableStream({
68
- start(controller) {
69
- startWork(request);
70
- },
71
- pull(controller) {
72
- // Pull is called immediately even if the stream is not passed to anything.
73
- // That's buffering too early. We want to start buffering once the stream
74
- // is actually used by something so we can give it the best result possible
75
- // at that point.
76
- if (stream.locked) {
77
- startFlowing(request, controller);
78
- }
79
- },
80
- cancel(reason) {},
42
+): Promise<ReadableStream> {
43
+ return new Promise((resolve, reject) => {
44
+ function onCompleteShell() {
45
+ const stream = new ReadableStream({
46
+ pull(controller) {
47
+ // Pull is called immediately even if the stream is not passed to anything.
48
+ // That's buffering too early. We want to start buffering once the stream
49
+ // is actually used by something so we can give it the best result possible
50
+ // at that point.
51
+ if (stream.locked) {
52
+ startFlowing(request, controller);
53
+ }
54
+ },
55
+ cancel(reason) {},
56
+ });
57
+ resolve(stream);
58
+ }
59
+ function onErrorShell(error: mixed) {
60
+ reject(error);
61
+ }
62
+ const request = createRequest(
63
+ children,
64
+ createResponseState(
65
+ options ? options.identifierPrefix : undefined,
66
+ options ? options.nonce : undefined,
67
+ options ? options.bootstrapScriptContent : undefined,
68
+ options ? options.bootstrapScripts : undefined,
69
+ options ? options.bootstrapModules : undefined,
70
+ ),
71
+ createRootFormatContext(options ? options.namespaceURI : undefined),
72
+ options ? options.progressiveChunkSize : undefined,
73
+ options ? options.onError : undefined,
74
+ options ? options.onCompleteAll : undefined,
75
+ onCompleteShell,
76
+ onErrorShell,
77
+ );
78
+ if (options && options.signal) {
79
+ const signal = options.signal;
80
+ const listener = () => {
81
+ abort(request);
82
+ signal.removeEventListener('abort', listener);
83
+ };
84
+ signal.addEventListener('abort', listener);
85
+ }
86
+ startWork(request);
87
});
82
- return stream;
88
}
89
90
export {renderToReadableStream, ReactVersion as version};