-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Flight] Move around the Server side a bit #17251
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* 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. | ||
* | ||
* @emails react-core | ||
* @jest-environment node | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// Polyfills for test environment | ||
global.ReadableStream = require('@mattiasbuelens/web-streams-polyfill/ponyfill/es6').ReadableStream; | ||
global.TextDecoder = require('util').TextDecoder; | ||
|
||
let Stream; | ||
let React; | ||
let ReactFlightDOMServer; | ||
let ReactFlightDOMClient; | ||
|
||
describe('ReactFlightDOM', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
Stream = require('stream'); | ||
React = require('react'); | ||
ReactFlightDOMServer = require('react-dom/unstable-flight-server'); | ||
ReactFlightDOMClient = require('react-dom/unstable-flight-client'); | ||
}); | ||
|
||
function getTestStream() { | ||
let writable = new Stream.PassThrough(); | ||
let readable = new ReadableStream({ | ||
start(controller) { | ||
writable.on('data', chunk => { | ||
controller.enqueue(chunk); | ||
}); | ||
writable.on('end', () => { | ||
controller.close(); | ||
}); | ||
}, | ||
}); | ||
return { | ||
writable, | ||
readable, | ||
}; | ||
} | ||
|
||
async function waitForSuspense(fn) { | ||
while (true) { | ||
try { | ||
return fn(); | ||
} catch (promise) { | ||
if (typeof promise.then === 'function') { | ||
await promise; | ||
} else { | ||
throw promise; | ||
} | ||
} | ||
} | ||
} | ||
|
||
it('should resolve HTML using Node streams', async () => { | ||
function Text({children}) { | ||
return <span>{children}</span>; | ||
} | ||
function HTML() { | ||
return ( | ||
<div> | ||
<Text>hello</Text> | ||
<Text>world</Text> | ||
</div> | ||
); | ||
} | ||
|
||
function App() { | ||
let model = { | ||
html: <HTML />, | ||
}; | ||
return model; | ||
} | ||
|
||
let {writable, readable} = getTestStream(); | ||
ReactFlightDOMServer.pipeToNodeWritable(<App />, writable); | ||
let result = ReactFlightDOMClient.readFromReadableStream(readable); | ||
await waitForSuspense(() => { | ||
expect(result.model).toEqual({ | ||
html: '<div><span>hello</span><span>world</span></div>', | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,37 +5,43 @@ | |
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
* @jest-environment node | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// Polyfills for test environment | ||
global.ReadableStream = require('@mattiasbuelens/web-streams-polyfill/ponyfill/es6').ReadableStream; | ||
global.TextEncoder = require('util').TextEncoder; | ||
global.TextDecoder = require('util').TextDecoder; | ||
|
||
let React; | ||
let ReactFlightDOMServer; | ||
let ReactFlightDOMClient; | ||
|
||
describe('ReactFlightDOM', () => { | ||
describe('ReactFlightDOMBrowser', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
React = require('react'); | ||
ReactFlightDOMServer = require('react-dom/unstable-flight-server.browser'); | ||
ReactFlightDOMClient = require('react-dom/unstable-flight-client'); | ||
}); | ||
|
||
async function readResult(stream) { | ||
let reader = stream.getReader(); | ||
let result = ''; | ||
async function waitForSuspense(fn) { | ||
while (true) { | ||
let {done, value} = await reader.read(); | ||
if (done) { | ||
return result; | ||
try { | ||
return fn(); | ||
} catch (promise) { | ||
if (typeof promise.then === 'function') { | ||
await promise; | ||
} else { | ||
throw promise; | ||
} | ||
} | ||
result += Buffer.from(value).toString('utf8'); | ||
} | ||
} | ||
|
||
it('should resolve HTML', async () => { | ||
it('should resolve HTML using W3C streams', async () => { | ||
function Text({children}) { | ||
return <span>{children}</span>; | ||
} | ||
|
@@ -48,14 +54,19 @@ describe('ReactFlightDOM', () => { | |
); | ||
} | ||
|
||
let model = { | ||
html: <HTML />, | ||
}; | ||
let stream = ReactFlightDOMServer.renderToReadableStream(model); | ||
jest.runAllTimers(); | ||
let result = JSON.parse(await readResult(stream)); | ||
expect(result).toEqual({ | ||
html: '<div><span>hello</span><span>world</span></div>', | ||
function App() { | ||
let model = { | ||
html: <HTML />, | ||
}; | ||
return model; | ||
} | ||
|
||
let stream = ReactFlightDOMServer.renderToReadableStream(<App />); | ||
let result = ReactFlightDOMClient.readFromReadableStream(stream); | ||
await waitForSuspense(() => { | ||
expect(result.model).toEqual({ | ||
html: '<div><span>hello</span><span>world</span></div>', | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||
}); | ||
}); | ||
}); |
57 changes: 0 additions & 57 deletions
57
packages/react-dom/src/__tests__/ReactFlightDOMNode-test.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind asserting we actually eventually got here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There’s going to be a hundred of these. I’m not going to assert in each one that the testing environment works.
Jest fails it the promise to the test never resolves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean the promise could resolve without the inner expect actually having run successfully. If there’s a bug in waitForSuspense.