GeezerVison is showing github.com — [Flight] Encode Symbols as special rows that can be referenced by mod… · react/react@76a6dbc
File treeExpand file treeCollapse file tree Open diff view settings react-transport-dom-relay/src react-transport-dom-webpack/src/__tests__ react-transport-native-relay/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-client/src/ReactFlightClient.js +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -132,6 +132,13 @@ function createErrorChunk(response: Response, error: Error): ErroredChunk { | 132 | 132 | return new Chunk(ERRORED, error, response); | 133 | 133 | } | 134 | 134 |
| | 135 | +function createInitializedChunk<T>( | | 136 | + response: Response, | | 137 | + value: T, | | 138 | +): InitializedChunk<T> { | | 139 | + return new Chunk(INITIALIZED, value, response); | | 140 | +} | | 141 | + | 135 | 142 | function wakeChunk(listeners: null | Array<() => mixed>) { | 136 | 143 | if (listeners !== null) { | 137 | 144 | for (let i = 0; i < listeners.length; i++) { | @@ -373,6 +380,17 @@ export function resolveModule( | 373 | 380 | } | 374 | 381 | } | 375 | 382 |
| | 383 | +export function resolveSymbol( | | 384 | + response: Response, | | 385 | + id: number, | | 386 | + name: string, | | 387 | +): void { | | 388 | + const chunks = response._chunks; | | 389 | + // We assume that we'll always emit the symbol before anything references it | | 390 | + // to save a few bytes. | | 391 | + chunks.set(id, createInitializedChunk(response, Symbol.for(name))); | | 392 | +} | | 393 | + | 376 | 394 | export function resolveError( | 377 | 395 | response: Response, | 378 | 396 | id: number, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-client/src/ReactFlightClientStream.js +15-12Lines changed: 15 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ import type {Response} from './ReactFlightClientHostConfigStream'; | 12 | 12 | import { | 13 | 13 | resolveModule, | 14 | 14 | resolveModel, | | 15 | + resolveSymbol, | 15 | 16 | resolveError, | 16 | 17 | createResponse as createResponseBase, | 17 | 18 | parseModelString, | @@ -32,26 +33,28 @@ function processFullRow(response: Response, row: string): void { | 32 | 33 | return; | 33 | 34 | } | 34 | 35 | const tag = row[0]; | | 36 | + // When tags that are not text are added, check them here before | | 37 | + // parsing the row as text. | | 38 | + // switch (tag) { | | 39 | + // } | | 40 | + const colon = row.indexOf(':', 1); | | 41 | + const id = parseInt(row.substring(1, colon), 16); | | 42 | + const text = row.substring(colon + 1); | 35 | 43 | switch (tag) { | 36 | 44 | case 'J': { | 37 | | - const colon = row.indexOf(':', 1); | 38 | | - const id = parseInt(row.substring(1, colon), 16); | 39 | | - const json = row.substring(colon + 1); | 40 | | - resolveModel(response, id, json); | | 45 | + resolveModel(response, id, text); | 41 | 46 | return; | 42 | 47 | } | 43 | 48 | case 'M': { | 44 | | - const colon = row.indexOf(':', 1); | 45 | | - const id = parseInt(row.substring(1, colon), 16); | 46 | | - const json = row.substring(colon + 1); | 47 | | - resolveModule(response, id, json); | | 49 | + resolveModule(response, id, text); | | 50 | + return; | | 51 | + } | | 52 | + case 'S': { | | 53 | + resolveSymbol(response, id, JSON.parse(text)); | 48 | 54 | return; | 49 | 55 | } | 50 | 56 | case 'E': { | 51 | | - const colon = row.indexOf(':', 1); | 52 | | - const id = parseInt(row.substring(1, colon), 16); | 53 | | - const json = row.substring(colon + 1); | 54 | | - const errorInfo = JSON.parse(json); | | 57 | + const errorInfo = JSON.parse(text); | 55 | 58 | resolveError(response, id, errorInfo.message, errorInfo.stack); | 56 | 59 | return; | 57 | 60 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-client/src/__tests__/ReactFlight-test.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -174,7 +174,7 @@ describe('ReactFlight', () => { | 174 | 174 | <ErrorBoundary expectedMessage="Functions cannot be passed directly to client components because they're not serializable."> | 175 | 175 | <Client transport={fn} /> | 176 | 176 | </ErrorBoundary> | 177 | | - <ErrorBoundary expectedMessage="Symbol values (foo) cannot be passed to client components."> | | 177 | + <ErrorBoundary expectedMessage="Only global symbols received from Symbol.for(...) can be passed to client components."> | 178 | 178 | <Client transport={symbol} /> | 179 | 179 | </ErrorBoundary> | 180 | 180 | <ErrorBoundary expectedMessage="Refs cannot be used in server components, nor passed to client components."> | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server/src/ReactFlightServer.js +55-35Lines changed: 55 additions & 35 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,28 +25,20 @@ import { | 25 | 25 | close, | 26 | 26 | processModelChunk, | 27 | 27 | processModuleChunk, | | 28 | + processSymbolChunk, | 28 | 29 | processErrorChunk, | 29 | 30 | resolveModuleMetaData, | 30 | 31 | isModuleReference, | 31 | 32 | } from './ReactFlightServerConfig'; | 32 | 33 |
| 33 | 34 | import { | 34 | 35 | REACT_ELEMENT_TYPE, | 35 | | - REACT_DEBUG_TRACING_MODE_TYPE, | 36 | 36 | REACT_FORWARD_REF_TYPE, | 37 | 37 | REACT_FRAGMENT_TYPE, | 38 | 38 | REACT_LAZY_TYPE, | 39 | | - REACT_LEGACY_HIDDEN_TYPE, | 40 | 39 | REACT_MEMO_TYPE, | 41 | | - REACT_OFFSCREEN_TYPE, | 42 | | - REACT_PROFILER_TYPE, | 43 | | - REACT_SCOPE_TYPE, | 44 | | - REACT_STRICT_MODE_TYPE, | 45 | | - REACT_SUSPENSE_TYPE, | 46 | | - REACT_SUSPENSE_LIST_TYPE, | 47 | 40 | } from 'shared/ReactSymbols'; | 48 | 41 |
| 49 | | -import * as React from 'react'; | 50 | 42 | import ReactSharedInternals from 'shared/ReactSharedInternals'; | 51 | 43 | import invariant from 'shared/invariant'; | 52 | 44 |
| @@ -86,6 +78,7 @@ export type Request = { | 86 | 78 | completedModuleChunks: Array<Chunk>, | 87 | 79 | completedJSONChunks: Array<Chunk>, | 88 | 80 | completedErrorChunks: Array<Chunk>, | | 81 | + writtenSymbols: Map<Symbol, number>, | 89 | 82 | flowing: boolean, | 90 | 83 | toJSON: (key: string, value: ReactModel) => ReactJSONValue, | 91 | 84 | }; | @@ -107,6 +100,7 @@ export function createRequest( | 107 | 100 | completedModuleChunks: [], | 108 | 101 | completedJSONChunks: [], | 109 | 102 | completedErrorChunks: [], | | 103 | + writtenSymbols: new Map(), | 110 | 104 | flowing: false, | 111 | 105 | toJSON: function(key: string, value: ReactModel): ReactJSONValue { | 112 | 106 | return resolveModelToJSON(request, this, key, value); | @@ -118,10 +112,13 @@ export function createRequest( | 118 | 112 | return request; | 119 | 113 | } | 120 | 114 |
| 121 | | -function attemptResolveElement(element: React$Element<any>): ReactModel { | 122 | | - const type = element.type; | 123 | | - const props = element.props; | 124 | | - if (element.ref !== null && element.ref !== undefined) { | | 115 | +function attemptResolveElement( | | 116 | + type: any, | | 117 | + key: null | React$Key, | | 118 | + ref: mixed, | | 119 | + props: any, | | 120 | +): ReactModel { | | 121 | + if (ref !== null && ref !== undefined) { | 125 | 122 | // When the ref moves to the regular props object this will implicitly | 126 | 123 | // throw for functions. We could probably relax it to a DEV warning for other | 127 | 124 | // cases. | @@ -135,34 +132,30 @@ function attemptResolveElement(element: React$Element<any>): ReactModel { | 135 | 132 | return type(props); | 136 | 133 | } else if (typeof type === 'string') { | 137 | 134 | // This is a host element. E.g. HTML. | 138 | | - return [REACT_ELEMENT_TYPE, type, element.key, element.props]; | 139 | | - } else if ( | 140 | | - type === REACT_FRAGMENT_TYPE || | 141 | | - type === REACT_STRICT_MODE_TYPE || | 142 | | - type === REACT_PROFILER_TYPE || | 143 | | - type === REACT_SCOPE_TYPE || | 144 | | - type === REACT_DEBUG_TRACING_MODE_TYPE || | 145 | | - type === REACT_LEGACY_HIDDEN_TYPE || | 146 | | - type === REACT_OFFSCREEN_TYPE || | 147 | | - // TODO: These are temporary shims | 148 | | - // and we'll want a different behavior. | 149 | | - type === REACT_SUSPENSE_TYPE || | 150 | | - type === REACT_SUSPENSE_LIST_TYPE | 151 | | - ) { | 152 | | - return element.props.children; | | 135 | + return [REACT_ELEMENT_TYPE, type, key, props]; | | 136 | + } else if (typeof type === 'symbol') { | | 137 | + if (type === REACT_FRAGMENT_TYPE) { | | 138 | + // For key-less fragments, we add a small optimization to avoid serializing | | 139 | + // it as a wrapper. | | 140 | + // TODO: If a key is specified, we should propagate its key to any children. | | 141 | + // Same as if a server component has a key. | | 142 | + return props.children; | | 143 | + } | | 144 | + // This might be a built-in React component. We'll let the client decide. | | 145 | + // Any built-in works as long as its props are serializable. | | 146 | + return [REACT_ELEMENT_TYPE, type, key, props]; | 153 | 147 | } else if (type != null && typeof type === 'object') { | 154 | 148 | if (isModuleReference(type)) { | 155 | 149 | // This is a reference to a client component. | 156 | | - return [REACT_ELEMENT_TYPE, type, element.key, element.props]; | | 150 | + return [REACT_ELEMENT_TYPE, type, key, props]; | 157 | 151 | } | 158 | 152 | switch (type.$$typeof) { | 159 | 153 | case REACT_FORWARD_REF_TYPE: { | 160 | 154 | const render = type.render; | 161 | 155 | return render(props, undefined); | 162 | 156 | } | 163 | 157 | case REACT_MEMO_TYPE: { | 164 | | - const nextChildren = React.createElement(type.type, element.props); | 165 | | - return attemptResolveElement(nextChildren); | | 158 | + return attemptResolveElement(type.type, key, ref, props); | 166 | 159 | } | 167 | 160 | } | 168 | 161 | } | @@ -399,7 +392,12 @@ export function resolveModelToJSON( | 399 | 392 | const element: React$Element<any> = (value: any); | 400 | 393 | try { | 401 | 394 | // Attempt to render the server component. | 402 | | - value = attemptResolveElement(element); | | 395 | + value = attemptResolveElement( | | 396 | + element.type, | | 397 | + element.key, | | 398 | + element.ref, | | 399 | + element.props, | | 400 | + ); | 403 | 401 | } catch (x) { | 404 | 402 | if (typeof x === 'object' && x !== null && typeof x.then === 'function') { | 405 | 403 | // Something suspended, we'll need to create a new segment and resolve it later. | @@ -526,14 +524,26 @@ export function resolveModelToJSON( | 526 | 524 | } | 527 | 525 |
| 528 | 526 | if (typeof value === 'symbol') { | | 527 | + const writtenSymbols = request.writtenSymbols; | | 528 | + const existingId = writtenSymbols.get(value); | | 529 | + if (existingId !== undefined) { | | 530 | + return serializeByValueID(existingId); | | 531 | + } | | 532 | + const name = value.description; | 529 | 533 | invariant( | 530 | | - false, | 531 | | - 'Symbol values (%s) cannot be passed to client components. ' + | | 534 | + Symbol.for(name) === value, | | 535 | + 'Only global symbols received from Symbol.for(...) can be passed to client components. ' + | | 536 | + 'The symbol Symbol.for(%s) cannot be found among global symbols. ' + | 532 | 537 | 'Remove %s from this object, or avoid the entire object: %s', | 533 | 538 | value.description, | 534 | 539 | describeKeyForErrorMessage(key), | 535 | 540 | describeObjectForErrorMessage(parent), | 536 | 541 | ); | | 542 | + request.pendingChunks++; | | 543 | + const symbolId = request.nextChunkId++; | | 544 | + emitSymbolChunk(request, symbolId, name); | | 545 | + writtenSymbols.set(value, symbolId); | | 546 | + return serializeByValueID(symbolId); | 537 | 547 | } | 538 | 548 |
| 539 | 549 | // $FlowFixMe: bigint isn't added to Flow yet. | @@ -588,6 +598,11 @@ function emitModuleChunk( | 588 | 598 | request.completedModuleChunks.push(processedChunk); | 589 | 599 | } | 590 | 600 |
| | 601 | +function emitSymbolChunk(request: Request, id: number, name: string): void { | | 602 | + const processedChunk = processSymbolChunk(request, id, name); | | 603 | + request.completedModuleChunks.push(processedChunk); | | 604 | +} | | 605 | + | 591 | 606 | function retrySegment(request: Request, segment: Segment): void { | 592 | 607 | const query = segment.query; | 593 | 608 | let value; | @@ -604,7 +619,12 @@ function retrySegment(request: Request, segment: Segment): void { | 604 | 619 | // Doing this here lets us reuse this same segment if the next component | 605 | 620 | // also suspends. | 606 | 621 | segment.query = () => value; | 607 | | - value = attemptResolveElement(element); | | 622 | + value = attemptResolveElement( | | 623 | + element.type, | | 624 | + element.key, | | 625 | + element.ref, | | 626 | + element.props, | | 627 | + ); | 608 | 628 | } | 609 | 629 | const processedChunk = processModelChunk(request, segment.id, value); | 610 | 630 | request.completedJSONChunks.push(processedChunk); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-server/src/ReactFlightServerConfigStream.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -109,6 +109,16 @@ export function processModuleChunk( | 109 | 109 | return convertStringToBuffer(row); | 110 | 110 | } | 111 | 111 |
| | 112 | +export function processSymbolChunk( | | 113 | + request: Request, | | 114 | + id: number, | | 115 | + name: string, | | 116 | +): Chunk { | | 117 | + const json = stringify(name); | | 118 | + const row = serializeRowHeader('S', id) + json + '\n'; | | 119 | + return convertStringToBuffer(row); | | 120 | +} | | 121 | + | 112 | 122 | export { | 113 | 123 | scheduleWork, | 114 | 124 | flushBuffered, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-relay/src/ReactFlightDOMRelayClient.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,6 +15,7 @@ import { | 15 | 15 | createResponse, | 16 | 16 | resolveModel, | 17 | 17 | resolveModule, | | 18 | + resolveSymbol, | 18 | 19 | resolveError, | 19 | 20 | close, | 20 | 21 | } from 'react-client/src/ReactFlightClient'; | @@ -26,6 +27,9 @@ export function resolveRow(response: Response, chunk: RowEncoding): void { | 26 | 27 | resolveModel(response, chunk[1], chunk[2]); | 27 | 28 | } else if (chunk[0] === 'M') { | 28 | 29 | resolveModule(response, chunk[1], chunk[2]); | | 30 | + } else if (chunk[0] === 'S') { | | 31 | + // $FlowFixMe: Flow doesn't support disjoint unions on tuples. | | 32 | + resolveSymbol(response, chunk[1], chunk[2]); | 29 | 33 | } else { | 30 | 34 | // $FlowFixMe: Flow doesn't support disjoint unions on tuples. | 31 | 35 | resolveError(response, chunk[1], chunk[2].message, chunk[2].stack); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-relay/src/ReactFlightDOMRelayProtocol.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,6 +20,7 @@ export type JSONValue = | 20 | 20 | export type RowEncoding = | 21 | 21 | | ['J', number, JSONValue] | 22 | 22 | | ['M', number, ModuleMetaData] | | 23 | + | ['S', number, string] | 23 | 24 | | [ | 24 | 25 | 'E', | 25 | 26 | number, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-relay/src/ReactFlightDOMRelayServerHostConfig.js +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -111,6 +111,14 @@ export function processModuleChunk( | 111 | 111 | return ['M', id, moduleMetaData]; | 112 | 112 | } | 113 | 113 |
| | 114 | +export function processSymbolChunk( | | 115 | + request: Request, | | 116 | + id: number, | | 117 | + name: string, | | 118 | +): Chunk { | | 119 | + return ['S', id, name]; | | 120 | +} | | 121 | + | 114 | 122 | export function scheduleWork(callback: () => void) { | 115 | 123 | callback(); | 116 | 124 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-relay/src/__tests__/ReactFlightDOMRelay-test.internal.js +8-5Lines changed: 8 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -153,11 +153,14 @@ describe('ReactFlightDOMRelay', () => { | 153 | 153 | foo: { | 154 | 154 | bar: ( | 155 | 155 | <div> | 156 | | - {'Fragment child'} | 157 | | - {'Profiler child'} | 158 | | - {'StrictMode child'} | 159 | | - {'Suspense child'} | 160 | | - {['SuspenseList row 1', 'SuspenseList row 2']} | | 156 | + Fragment child | | 157 | + <Profiler>Profiler child</Profiler> | | 158 | + <StrictMode>StrictMode child</StrictMode> | | 159 | + <Suspense fallback="Loading...">Suspense child</Suspense> | | 160 | + <SuspenseList fallback="Loading..."> | | 161 | + {'SuspenseList row 1'} | | 162 | + {'SuspenseList row 2'} | | 163 | + </SuspenseList> | 161 | 164 | <div>Hello world</div> | 162 | 165 | </div> | 163 | 166 | ), | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-webpack/src/__tests__/ReactFlightDOM-test.js +6-11Lines changed: 6 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -282,10 +282,6 @@ describe('ReactFlightDOM', () => { | 282 | 282 | ); | 283 | 283 | } | 284 | 284 |
| 285 | | - function Placeholder({children, fallback}) { | 286 | | - return <Suspense fallback={fallback}>{children}</Suspense>; | 287 | | - } | 288 | | - | 289 | 285 | // Model | 290 | 286 | function Text({children}) { | 291 | 287 | return children; | @@ -347,22 +343,21 @@ describe('ReactFlightDOM', () => { | 347 | 343 | } | 348 | 344 |
| 349 | 345 | const MyErrorBoundaryClient = moduleReference(MyErrorBoundary); | 350 | | - const PlaceholderClient = moduleReference(Placeholder); | 351 | 346 |
| 352 | 347 | function ProfileContent() { | 353 | 348 | return ( | 354 | 349 | <> | 355 | 350 | <ProfileDetails avatar={<Text>:avatar:</Text>} /> | 356 | | - <PlaceholderClient fallback={<p>(loading sidebar)</p>}> | | 351 | + <Suspense fallback={<p>(loading sidebar)</p>}> | 357 | 352 | <ProfileSidebar friends={<Friends>:friends:</Friends>} /> | 358 | | - </PlaceholderClient> | 359 | | - <PlaceholderClient fallback={<p>(loading posts)</p>}> | | 353 | + </Suspense> | | 354 | + <Suspense fallback={<p>(loading posts)</p>}> | 360 | 355 | <ProfilePosts posts={<Posts>:posts:</Posts>} /> | 361 | | - </PlaceholderClient> | | 356 | + </Suspense> | 362 | 357 | <MyErrorBoundaryClient> | 363 | | - <PlaceholderClient fallback={<p>(loading games)</p>}> | | 358 | + <Suspense fallback={<p>(loading games)</p>}> | 364 | 359 | <ProfileGames games={<Games>:games:</Games>} /> | 365 | | - </PlaceholderClient> | | 360 | + </Suspense> | 366 | 361 | </MyErrorBoundaryClient> | 367 | 362 | </> | 368 | 363 | ); | |
|