forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fizz] Add FB specific streaming API and build (facebook#21337)
Add FB specific streaming API and build
- Loading branch information
1 parent
af5037a
commit 709f948
Showing
8 changed files
with
342 additions
and
14 deletions.
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,87 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
import type {ReactNodeList} from 'shared/ReactTypes'; | ||
|
||
import type {Request} from 'react-server/src/ReactFizzServer'; | ||
|
||
import type {Destination} from 'react-server/src/ReactServerStreamConfig'; | ||
|
||
import { | ||
createRequest, | ||
startWork, | ||
performWork, | ||
startFlowing, | ||
abort, | ||
} from 'react-server/src/ReactFizzServer'; | ||
|
||
import { | ||
createResponseState, | ||
createRootFormatContext, | ||
} from 'react-server/src/ReactServerFormatConfig'; | ||
|
||
type Options = { | ||
identifierPrefix?: string, | ||
progressiveChunkSize?: number, | ||
onError: (error: mixed) => void, | ||
}; | ||
|
||
opaque type Stream = { | ||
destination: Destination, | ||
request: Request, | ||
}; | ||
|
||
function renderToStream(children: ReactNodeList, options: Options): Stream { | ||
const destination = { | ||
buffer: '', | ||
done: false, | ||
fatal: false, | ||
error: null, | ||
}; | ||
const request = createRequest( | ||
children, | ||
destination, | ||
createResponseState(options ? options.identifierPrefix : undefined), | ||
createRootFormatContext(undefined), | ||
options ? options.progressiveChunkSize : undefined, | ||
options.onError, | ||
undefined, | ||
undefined, | ||
); | ||
startWork(request); | ||
if (destination.fatal) { | ||
throw destination.error; | ||
} | ||
return { | ||
destination, | ||
request, | ||
}; | ||
} | ||
|
||
function abortStream(stream: Stream): void { | ||
abort(stream.request); | ||
} | ||
|
||
function renderNextChunk(stream: Stream): string { | ||
const {request, destination} = stream; | ||
performWork(request); | ||
startFlowing(request); | ||
if (destination.fatal) { | ||
throw destination.error; | ||
} | ||
const chunk = destination.buffer; | ||
destination.buffer = ''; | ||
return chunk; | ||
} | ||
|
||
function hasFinished(stream: Stream): boolean { | ||
return stream.destination.done; | ||
} | ||
|
||
export {renderToStream, renderNextChunk, hasFinished, abortStream}; |
54 changes: 54 additions & 0 deletions
54
packages/react-server-dom-relay/src/ReactServerStreamConfigFB.js
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,54 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
export type Destination = { | ||
buffer: string, | ||
done: boolean, | ||
fatal: boolean, | ||
error: mixed, | ||
}; | ||
|
||
export type PrecomputedChunk = string; | ||
export type Chunk = string; | ||
|
||
export function scheduleWork(callback: () => void) { | ||
// We don't schedule work in this model, and instead expect performWork to always be called repeatedly. | ||
} | ||
|
||
export function flushBuffered(destination: Destination) {} | ||
|
||
export function beginWriting(destination: Destination) {} | ||
|
||
export function writeChunk( | ||
destination: Destination, | ||
chunk: Chunk | PrecomputedChunk, | ||
): boolean { | ||
destination.buffer += chunk; | ||
return true; | ||
} | ||
|
||
export function completeWriting(destination: Destination) {} | ||
|
||
export function close(destination: Destination) { | ||
destination.done = true; | ||
} | ||
|
||
export function stringToChunk(content: string): Chunk { | ||
return content; | ||
} | ||
|
||
export function stringToPrecomputedChunk(content: string): PrecomputedChunk { | ||
return content; | ||
} | ||
|
||
export function closeWithError(destination: Destination, error: mixed): void { | ||
destination.done = true; | ||
destination.fatal = true; | ||
destination.error = error; | ||
} |
182 changes: 182 additions & 0 deletions
182
packages/react-server-dom-relay/src/__tests__/ReactDOMServerFB-test.internal.js
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,182 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let React; | ||
let ReactDOMServer; | ||
let Suspense; | ||
|
||
describe('ReactDOMServerFB', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
React = require('react'); | ||
ReactDOMServer = require('../ReactDOMServerFB'); | ||
Suspense = React.Suspense; | ||
}); | ||
|
||
const theError = new Error('This is an error'); | ||
function Throw() { | ||
throw theError; | ||
} | ||
const theInfinitePromise = new Promise(() => {}); | ||
function InfiniteSuspend() { | ||
throw theInfinitePromise; | ||
} | ||
|
||
function readResult(stream) { | ||
let result = ''; | ||
while (!ReactDOMServer.hasFinished(stream)) { | ||
result += ReactDOMServer.renderNextChunk(stream); | ||
} | ||
return result; | ||
} | ||
|
||
it('should be able to render basic HTML', async () => { | ||
const stream = ReactDOMServer.renderToStream(<div>hello world</div>, { | ||
onError(x) { | ||
console.error(x); | ||
}, | ||
}); | ||
const result = readResult(stream); | ||
expect(result).toMatchInlineSnapshot( | ||
`"<div data-reactroot=\\"\\">hello world</div>"`, | ||
); | ||
}); | ||
|
||
it('emits all HTML as one unit if we wait until the end to start', async () => { | ||
let hasLoaded = false; | ||
let resolve; | ||
const promise = new Promise(r => (resolve = r)); | ||
function Wait() { | ||
if (!hasLoaded) { | ||
throw promise; | ||
} | ||
return 'Done'; | ||
} | ||
const stream = ReactDOMServer.renderToStream( | ||
<div> | ||
<Suspense fallback="Loading"> | ||
<Wait /> | ||
</Suspense> | ||
</div>, | ||
{ | ||
onError(x) { | ||
console.error(x); | ||
}, | ||
}, | ||
); | ||
await jest.runAllTimers(); | ||
// Resolve the loading. | ||
hasLoaded = true; | ||
await resolve(); | ||
|
||
await jest.runAllTimers(); | ||
|
||
const result = readResult(stream); | ||
expect(result).toMatchInlineSnapshot( | ||
`"<div data-reactroot=\\"\\"><!--$-->Done<!-- --><!--/$--></div>"`, | ||
); | ||
}); | ||
|
||
it('should throw an error when an error is thrown at the root', () => { | ||
const reportedErrors = []; | ||
const stream = ReactDOMServer.renderToStream( | ||
<div> | ||
<Throw /> | ||
</div>, | ||
{ | ||
onError(x) { | ||
reportedErrors.push(x); | ||
}, | ||
}, | ||
); | ||
|
||
let caughtError = null; | ||
let result = ''; | ||
try { | ||
result = readResult(stream); | ||
} catch (x) { | ||
caughtError = x; | ||
} | ||
expect(caughtError).toBe(theError); | ||
expect(result).toBe(''); | ||
expect(reportedErrors).toEqual([theError]); | ||
}); | ||
|
||
it('should throw an error when an error is thrown inside a fallback', () => { | ||
const reportedErrors = []; | ||
const stream = ReactDOMServer.renderToStream( | ||
<div> | ||
<Suspense fallback={<Throw />}> | ||
<InfiniteSuspend /> | ||
</Suspense> | ||
</div>, | ||
{ | ||
onError(x) { | ||
reportedErrors.push(x); | ||
}, | ||
}, | ||
); | ||
|
||
let caughtError = null; | ||
let result = ''; | ||
try { | ||
result = readResult(stream); | ||
} catch (x) { | ||
caughtError = x; | ||
} | ||
expect(caughtError).toBe(theError); | ||
expect(result).toBe(''); | ||
expect(reportedErrors).toEqual([theError]); | ||
}); | ||
|
||
it('should not throw an error when an error is thrown inside suspense boundary', async () => { | ||
const reportedErrors = []; | ||
const stream = ReactDOMServer.renderToStream( | ||
<div> | ||
<Suspense fallback={<div>Loading</div>}> | ||
<Throw /> | ||
</Suspense> | ||
</div>, | ||
{ | ||
onError(x) { | ||
reportedErrors.push(x); | ||
}, | ||
}, | ||
); | ||
|
||
const result = readResult(stream); | ||
expect(result).toContain('Loading'); | ||
expect(reportedErrors).toEqual([theError]); | ||
}); | ||
|
||
it('should be able to complete by aborting even if the promise never resolves', () => { | ||
const stream = ReactDOMServer.renderToStream( | ||
<div> | ||
<Suspense fallback={<div>Loading</div>}> | ||
<InfiniteSuspend /> | ||
</Suspense> | ||
</div>, | ||
{ | ||
onError(x) { | ||
console.error(x); | ||
}, | ||
}, | ||
); | ||
|
||
const partial = ReactDOMServer.renderNextChunk(stream); | ||
expect(partial).toContain('Loading'); | ||
|
||
ReactDOMServer.abortStream(stream); | ||
|
||
const remaining = readResult(stream); | ||
expect(remaining).toEqual(''); | ||
}); | ||
}); |
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.