GeezerVison is showing github.com — [Fizz] Expose a method to explicitly start writing to a Node stream (… · react/react@154b852
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js +8-2Lines changed: 8 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -196,14 +196,15 @@ describe('ReactDOMFizzServer', () => { | 196 | 196 | // @gate experimental | 197 | 197 | it('should asynchronously load the suspense boundary', async () => { | 198 | 198 | await act(async () => { | 199 | | - ReactDOMFizzServer.pipeToNodeWritable( | | 199 | + const {startWriting} = ReactDOMFizzServer.pipeToNodeWritable( | 200 | 200 | <div> | 201 | 201 | <Suspense fallback={<Text text="Loading..." />}> | 202 | 202 | <AsyncText text="Hello World" /> | 203 | 203 | </Suspense> | 204 | 204 | </div>, | 205 | 205 | writable, | 206 | 206 | ); | | 207 | + startWriting(); | 207 | 208 | }); | 208 | 209 | expect(getVisibleChildren(container)).toEqual(<div>Loading...</div>); | 209 | 210 | await act(async () => { | @@ -229,7 +230,11 @@ describe('ReactDOMFizzServer', () => { | 229 | 230 | } | 230 | 231 |
| 231 | 232 | await act(async () => { | 232 | | - ReactDOMFizzServer.pipeToNodeWritable(<App />, writable); | | 233 | + const {startWriting} = ReactDOMFizzServer.pipeToNodeWritable( | | 234 | + <App />, | | 235 | + writable, | | 236 | + ); | | 237 | + startWriting(); | 233 | 238 | }); | 234 | 239 |
| 235 | 240 | // We're still showing a fallback. | @@ -281,6 +286,7 @@ describe('ReactDOMFizzServer', () => { | 281 | 286 | let controls; | 282 | 287 | await act(async () => { | 283 | 288 | controls = ReactDOMFizzServer.pipeToNodeWritable(<App />, writable); | | 289 | + controls.startWriting(); | 284 | 290 | }); | 285 | 291 |
| 286 | 292 | // We're still showing a fallback. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMFizzServerNode-test.js +33-5Lines changed: 33 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,11 +59,33 @@ describe('ReactDOMFizzServer', () => { | 59 | 59 | // @gate experimental | 60 | 60 | it('should call pipeToNodeWritable', () => { | 61 | 61 | const {writable, output} = getTestWritable(); | 62 | | - ReactDOMFizzServer.pipeToNodeWritable(<div>hello world</div>, writable); | | 62 | + const {startWriting} = ReactDOMFizzServer.pipeToNodeWritable( | | 63 | + <div>hello world</div>, | | 64 | + writable, | | 65 | + ); | | 66 | + startWriting(); | 63 | 67 | jest.runAllTimers(); | 64 | 68 | expect(output.result).toBe('<div>hello world</div>'); | 65 | 69 | }); | 66 | 70 |
| | 71 | + // @gate experimental | | 72 | + it('should start writing after startWriting', () => { | | 73 | + const {writable, output} = getTestWritable(); | | 74 | + const {startWriting} = ReactDOMFizzServer.pipeToNodeWritable( | | 75 | + <div>hello world</div>, | | 76 | + writable, | | 77 | + ); | | 78 | + jest.runAllTimers(); | | 79 | + // First we write our header. | | 80 | + output.result += | | 81 | + '<!doctype html><html><head><title>test</title><head><body>'; | | 82 | + // Then React starts writing. | | 83 | + startWriting(); | | 84 | + expect(output.result).toBe( | | 85 | + '<!doctype html><html><head><title>test</title><head><body><div>hello world</div>', | | 86 | + ); | | 87 | + }); | | 88 | + | 67 | 89 | // @gate experimental | 68 | 90 | it('should error the stream when an error is thrown at the root', async () => { | 69 | 91 | const {writable, output, completed} = getTestWritable(); | @@ -74,6 +96,8 @@ describe('ReactDOMFizzServer', () => { | 74 | 96 | writable, | 75 | 97 | ); | 76 | 98 |
| | 99 | + // The stream is errored even if we haven't started writing. | | 100 | + | 77 | 101 | await completed; | 78 | 102 |
| 79 | 103 | expect(output.error).toBe(theError); | @@ -83,14 +107,15 @@ describe('ReactDOMFizzServer', () => { | 83 | 107 | // @gate experimental | 84 | 108 | it('should error the stream when an error is thrown inside a fallback', async () => { | 85 | 109 | const {writable, output, completed} = getTestWritable(); | 86 | | - ReactDOMFizzServer.pipeToNodeWritable( | | 110 | + const {startWriting} = ReactDOMFizzServer.pipeToNodeWritable( | 87 | 111 | <div> | 88 | 112 | <Suspense fallback={<Throw />}> | 89 | 113 | <InfiniteSuspend /> | 90 | 114 | </Suspense> | 91 | 115 | </div>, | 92 | 116 | writable, | 93 | 117 | ); | | 118 | + startWriting(); | 94 | 119 |
| 95 | 120 | await completed; | 96 | 121 |
| @@ -101,14 +126,15 @@ describe('ReactDOMFizzServer', () => { | 101 | 126 | // @gate experimental | 102 | 127 | it('should not error the stream when an error is thrown inside suspense boundary', async () => { | 103 | 128 | const {writable, output, completed} = getTestWritable(); | 104 | | - ReactDOMFizzServer.pipeToNodeWritable( | | 129 | + const {startWriting} = ReactDOMFizzServer.pipeToNodeWritable( | 105 | 130 | <div> | 106 | 131 | <Suspense fallback={<div>Loading</div>}> | 107 | 132 | <Throw /> | 108 | 133 | </Suspense> | 109 | 134 | </div>, | 110 | 135 | writable, | 111 | 136 | ); | | 137 | + startWriting(); | 112 | 138 |
| 113 | 139 | await completed; | 114 | 140 |
| @@ -128,12 +154,13 @@ describe('ReactDOMFizzServer', () => { | 128 | 154 | function Content() { | 129 | 155 | return 'Hi'; | 130 | 156 | } | 131 | | - ReactDOMFizzServer.pipeToNodeWritable( | | 157 | + const {startWriting} = ReactDOMFizzServer.pipeToNodeWritable( | 132 | 158 | <Suspense fallback={<Fallback />}> | 133 | 159 | <Content /> | 134 | 160 | </Suspense>, | 135 | 161 | writable, | 136 | 162 | ); | | 163 | + startWriting(); | 137 | 164 |
| 138 | 165 | await completed; | 139 | 166 |
| @@ -145,14 +172,15 @@ describe('ReactDOMFizzServer', () => { | 145 | 172 | // @gate experimental | 146 | 173 | it('should be able to complete by aborting even if the promise never resolves', async () => { | 147 | 174 | const {writable, output, completed} = getTestWritable(); | 148 | | - const {abort} = ReactDOMFizzServer.pipeToNodeWritable( | | 175 | + const {startWriting, abort} = ReactDOMFizzServer.pipeToNodeWritable( | 149 | 176 | <div> | 150 | 177 | <Suspense fallback={<div>Loading</div>}> | 151 | 178 | <InfiniteSuspend /> | 152 | 179 | </Suspense> | 153 | 180 | </div>, | 154 | 181 | writable, | 155 | 182 | ); | | 183 | + startWriting(); | 156 | 184 |
| 157 | 185 | jest.runAllTimers(); | 158 | 186 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/server/ReactDOMFizzServerNode.js +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,9 +32,17 @@ function pipeToNodeWritable( | 32 | 32 | destination: Writable, | 33 | 33 | ): Controls { | 34 | 34 | const request = createRequest(children, destination); | 35 | | - destination.on('drain', createDrainHandler(destination, request)); | | 35 | + let hasStartedFlowing = false; | 36 | 36 | startWork(request); | 37 | 37 | return { | | 38 | + startWriting() { | | 39 | + if (hasStartedFlowing) { | | 40 | + return; | | 41 | + } | | 42 | + hasStartedFlowing = true; | | 43 | + startFlowing(request); | | 44 | + destination.on('drain', createDrainHandler(destination, request)); | | 45 | + }, | 38 | 46 | abort() { | 39 | 47 | abort(request); | 40 | 48 | }, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-noop-renderer/src/ReactNoopServer.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -222,6 +222,7 @@ function render(children: React$Element<any>): Destination { | 222 | 222 | }; | 223 | 223 | const request = ReactNoopServer.createRequest(children, destination); | 224 | 224 | ReactNoopServer.startWork(request); | | 225 | + ReactNoopServer.startFlowing(request); | 225 | 226 | return destination; | 226 | 227 | } | 227 | 228 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server/src/ReactFizzServer.js -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -894,8 +894,6 @@ function flushCompletedQueues(request: Request): void { | 894 | 894 | // This would put all waiting boundaries into client-only mode. | 895 | 895 |
| 896 | 896 | export function startWork(request: Request): void { | 897 | | - // TODO: Don't automatically start flowing. Expose an explicit signal. Auto-start once everything is done. | 898 | | - request.status = FLOWING; | 899 | 897 | scheduleWork(() => performWork(request)); | 900 | 898 | } | 901 | 899 |
| |
|