Skip to content

Commit a290681

Browse files
committed
Simplify interface
1 parent eedc2de commit a290681

File tree

6 files changed

+112
-51
lines changed

6 files changed

+112
-51
lines changed

packages/react-dom/src/server/ReactDOMFizzServerNext.js

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
createRequest,
1717
startWork,
1818
startFlowing,
19-
stopFlowing,
2019
abort,
2120
} from 'react-server/src/ReactFizzServer';
2221

@@ -25,56 +24,63 @@ import {
2524
createRootFormatContext,
2625
} from './ReactDOMServerFormatConfig';
2726

27+
type NextStreamSource = {
28+
start: (controller: Destination) => void,
29+
pull: (controller: Destination) => void,
30+
cancel: (reason: mixed) => void,
31+
};
32+
2833
type Options = {|
2934
identifierPrefix?: string,
3035
namespaceURI?: string,
36+
nonce?: string,
37+
bootstrapScriptContent?: string,
38+
bootstrapScripts?: Array<string>,
39+
bootstrapModules?: Array<string>,
3140
progressiveChunkSize?: number,
32-
onReadyToStream?: () => void,
41+
signal?: AbortSignal,
42+
onCompleteShell?: () => void,
3343
onCompleteAll?: () => void,
3444
onError?: (error: mixed) => void,
3545
|};
3646

37-
type Controls = {|
38-
abort(): void,
39-
update(): void,
40-
|};
41-
42-
function createRequestImpl(
47+
function renderToNextStream(
4348
children: ReactNodeList,
44-
destination: Destination,
45-
options: void | Options,
46-
) {
47-
return createRequest(
49+
options?: Options,
50+
): NextStreamSource {
51+
const request = createRequest(
4852
children,
49-
destination,
50-
createResponseState(options ? options.identifierPrefix : undefined),
53+
createResponseState(
54+
options ? options.identifierPrefix : undefined,
55+
options ? options.nonce : undefined,
56+
options ? options.bootstrapScriptContent : undefined,
57+
options ? options.bootstrapScripts : undefined,
58+
options ? options.bootstrapModules : undefined,
59+
),
5160
createRootFormatContext(options ? options.namespaceURI : undefined),
5261
options ? options.progressiveChunkSize : undefined,
5362
options ? options.onError : undefined,
5463
options ? options.onCompleteAll : undefined,
55-
options ? options.onReadyToStream : undefined,
64+
options ? options.onCompleteShell : undefined,
5665
);
57-
}
58-
59-
function renderToNextStream(
60-
children: ReactNodeList,
61-
destination: Destination,
62-
options?: Options,
63-
): Controls {
64-
const request = createRequestImpl(children, destination, options);
65-
startWork(request);
66-
return {
67-
abort() {
66+
if (options && options.signal) {
67+
const signal = options.signal;
68+
const listener = () => {
6869
abort(request);
70+
signal.removeEventListener('abort', listener);
71+
};
72+
signal.addEventListener('abort', listener);
73+
}
74+
const stream = {
75+
start(controller) {
76+
startWork(request);
6977
},
70-
update() {
71-
if (destination.ready) {
72-
startFlowing(request);
73-
} else {
74-
stopFlowing(request);
75-
}
78+
pull(controller) {
79+
startFlowing(request, controller);
7680
},
81+
cancel(reason) {},
7782
};
83+
return stream;
7884
}
7985

8086
export {renderToNextStream, ReactVersion as version};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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 type {ReactModel} from 'react-server/src/ReactFlightServer';
11+
import type {BundlerConfig} from './ReactFlightServerWebpackBundlerConfig';
12+
import type {Destination} from 'react-server/src/ReactServerStreamConfigNext';
13+
14+
import {
15+
createRequest,
16+
startWork,
17+
startFlowing,
18+
} from 'react-server/src/ReactFlightServer';
19+
20+
type NextStreamSource = {
21+
start: (controller: Destination) => void,
22+
pull: (controller: Destination) => void,
23+
cancel: (reason: mixed) => void,
24+
};
25+
26+
type Options = {
27+
onError?: (error: mixed) => void,
28+
};
29+
30+
function renderToNextStream(
31+
model: ReactModel,
32+
webpackMap: BundlerConfig,
33+
options?: Options,
34+
): NextStreamSource {
35+
const request = createRequest(
36+
model,
37+
webpackMap,
38+
options ? options.onError : undefined,
39+
);
40+
const stream = {
41+
start(controller) {
42+
startWork(request);
43+
},
44+
pull(controller) {
45+
startFlowing(request, controller);
46+
},
47+
cancel(reason) {},
48+
};
49+
return stream;
50+
}
51+
52+
export {renderToNextStream};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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+
export * from './src/ReactFlightDOMServerNext.js';

packages/react-server/src/ReactFizzServer.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,13 +1950,6 @@ export function startFlowing(request: Request, destination: Destination): void {
19501950
}
19511951
}
19521952

1953-
export function stopFlowing(request: Request): void {
1954-
if (request.status === CLOSED) {
1955-
return;
1956-
}
1957-
request.status = BUFFERING;
1958-
}
1959-
19601953
// This is called to early terminate a request. It puts all pending boundaries in client rendered state.
19611954
export function abort(request: Request): void {
19621955
try {

packages/react-server/src/ReactServerStreamConfigNext.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,21 @@
77
* @flow
88
*/
99

10-
declare function __next_scheduleReactServerStreamWork__(
11-
callback: () => void,
12-
): void;
10+
declare function __next_scheduleReactServerWork(callback: () => void): void;
1311

14-
export type Destination = {|
15-
write: (chunk: Uint8Array) => void,
16-
buffer: (shouldBuffer: boolean) => void,
12+
export type Destination = {
1713
flush: () => void,
14+
buffer: (buffer: boolean) => void,
1815
close: (error: mixed) => void,
19-
ready: boolean,
20-
|};
16+
write: (chunk: Uint8Array) => void,
17+
desiredSize: number,
18+
};
2119

2220
export type PrecomputedChunk = Uint8Array;
2321
export type Chunk = Uint8Array;
2422

2523
export function scheduleWork(callback: () => void) {
26-
__next_scheduleReactServerStreamWork__(callback);
24+
__next_scheduleReactServerWork(callback);
2725
}
2826

2927
export function flushBuffered(destination: Destination) {
@@ -36,10 +34,10 @@ export function beginWriting(destination: Destination) {
3634

3735
export function writeChunk(
3836
destination: Destination,
39-
chunk: PrecomputedChunk | Chunk,
37+
chunk: Chunk | PrecomputedChunk,
4038
): boolean {
4139
destination.write(chunk);
42-
return destination.ready;
40+
return destination.desiredSize > 0;
4341
}
4442

4543
export function completeWriting(destination: Destination) {

scripts/shared/inlinedHostConfigs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ module.exports = [
6969
{
7070
shortName: 'dom-next',
7171
entryPoints: [
72+
'react-server-dom-webpack/writer.next.server',
73+
'react-server-dom-webpack',
7274
'react-dom/src/server/ReactDOMFizzServerNext', // react-dom/server
7375
],
7476
paths: [
7577
'react-dom',
7678
'react-server-dom-webpack',
77-
'react-dom/src/server/ReactDOMFizzServerNext.js', // react-dom/server.browser
79+
'react-dom/src/server/ReactDOMFizzServerNext.js', // react-dom/server.next
7880
'react-client/src/ReactFlightClientStream.js', // We can only type check this in streaming configurations.
7981
],
8082
isFlowTyped: true,

0 commit comments

Comments
 (0)