2019-10-30 05:45:47 +08:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2020-03-07 08:20:42 +08:00
|
|
|
import type {ReactModel} from 'react-server/src/ReactFlightServer';
|
2020-03-19 03:18:34 +08:00
|
|
|
import type {BundlerConfig} from './ReactFlightServerWebpackBundlerConfig';
|
2019-10-30 05:45:47 +08:00
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
createRequest,
|
|
|
|
|
startWork,
|
|
|
|
|
startFlowing,
|
2020-03-07 08:20:42 +08:00
|
|
|
} from 'react-server/src/ReactFlightServer';
|
2019-10-30 05:45:47 +08:00
|
|
|
|
2021-03-30 10:36:16 +08:00
|
|
|
type Options = {
|
|
|
|
|
onError?: (error: mixed) => void,
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-19 03:18:34 +08:00
|
|
|
function renderToReadableStream(
|
|
|
|
|
model: ReactModel,
|
|
|
|
|
webpackMap: BundlerConfig,
|
2021-03-30 10:36:16 +08:00
|
|
|
options?: Options,
|
2020-03-19 03:18:34 +08:00
|
|
|
): ReadableStream {
|
2021-09-29 06:32:09 +08:00
|
|
|
const request = createRequest(
|
|
|
|
|
model,
|
|
|
|
|
webpackMap,
|
|
|
|
|
options ? options.onError : undefined,
|
|
|
|
|
);
|
2021-09-28 08:47:56 +08:00
|
|
|
const stream = new ReadableStream({
|
2019-10-30 05:45:47 +08:00
|
|
|
start(controller) {
|
|
|
|
|
startWork(request);
|
|
|
|
|
},
|
|
|
|
|
pull(controller) {
|
2021-09-28 08:47:56 +08:00
|
|
|
// Pull is called immediately even if the stream is not passed to anything.
|
|
|
|
|
// That's buffering too early. We want to start buffering once the stream
|
|
|
|
|
// is actually used by something so we can give it the best result possible
|
|
|
|
|
// at that point.
|
|
|
|
|
if (stream.locked) {
|
2021-09-29 06:32:09 +08:00
|
|
|
startFlowing(request, controller);
|
2021-09-28 08:47:56 +08:00
|
|
|
}
|
2019-10-30 05:45:47 +08:00
|
|
|
},
|
|
|
|
|
cancel(reason) {},
|
|
|
|
|
});
|
2021-09-28 08:47:56 +08:00
|
|
|
return stream;
|
2019-10-30 05:45:47 +08:00
|
|
|
}
|
|
|
|
|
|
2020-02-28 09:18:55 +08:00
|
|
|
export {renderToReadableStream};
|