Move the Error creation to be lazy (#24728)

Creating an Error captures a stack trace which can be somewhat expensive.
We shouldn't do tthat always for every render.

This also ensures that the stack trace is more useful because you can
follow through the Node.js code to see the cause.
This commit is contained in:
Sebastian Markbåge authored and GitHub committed 2022-06-14 20:16:42 -04:00
1 parent 256aefbea1
commit 522f47345f
1 file changed
+6 -10
+6 -10
View File
@@ -29,7 +29,8 @@ function createDrainHandler(destination, request) {
}
function createAbortHandler(request, reason) {
return () => abort(request, reason);
// eslint-disable-next-line react-internal/prod-error-codes
return () => abort(request, new Error(reason));
}
type Options = {|
@@ -49,7 +50,7 @@ type Options = {|
type PipeableStream = {|
// Cancel any pending I/O and put anything remaining into
// client rendered mode.
abort(): void,
abort(reason: mixed): void,
pipe<T: Writable>(destination: T): T,
|};
@@ -94,21 +95,16 @@ function renderToPipeableStream(
'error',
createAbortHandler(
request,
// eslint-disable-next-line react-internal/prod-error-codes
new Error('The destination stream errored while writing data.'),
'The destination stream errored while writing data.',
),
);
destination.on(
'close',
createAbortHandler(
request,
// eslint-disable-next-line react-internal/prod-error-codes
new Error('The destination stream closed early.'),
),
createAbortHandler(request, 'The destination stream closed early.'),
);
return destination;
},
abort(reason) {
abort(reason: mixed) {
abort(request, reason);
},
};