GeezerVison is showing github.com — [Flight] Simplify Relay row protocol (#20168) · react/react@35e53b4
File treeExpand file treeCollapse file tree Open diff view settings react-transport-dom-relay/src 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-transport-dom-relay/src/ReactFlightDOMRelayClient.js +18-1Lines changed: 18 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | * @flow | 8 | 8 | */ | 9 | 9 |
| 10 | | -export { | | 10 | +import type {RowEncoding} from './ReactFlightDOMRelayProtocol'; | | 11 | + | | 12 | +import type {Response} from 'react-client/src/ReactFlightClient'; | | 13 | + | | 14 | +import { | 11 | 15 | createResponse, | 12 | 16 | resolveModel, | 13 | 17 | resolveModule, | 14 | 18 | resolveError, | 15 | 19 | close, | 16 | 20 | } from 'react-client/src/ReactFlightClient'; | | 21 | + | | 22 | +export {createResponse, close}; | | 23 | + | | 24 | +export function resolveRow(response: Response, chunk: RowEncoding): void { | | 25 | + if (chunk[0] === 'J') { | | 26 | + resolveModel(response, chunk[1], chunk[2]); | | 27 | + } else if (chunk[0] === 'M') { | | 28 | + resolveModule(response, chunk[1], chunk[2]); | | 29 | + } else { | | 30 | + // $FlowFixMe: Flow doesn't support disjoint unions on tuples. | | 31 | + resolveError(response, chunk[1], chunk[2].message, chunk[2].stack); | | 32 | + } | | 33 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-relay/src/ReactFlightDOMRelayClientHostConfig.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,7 +26,7 @@ export { | 26 | 26 |
| 27 | 27 | export type {ModuleMetaData} from 'ReactFlightDOMRelayClientIntegration'; | 28 | 28 |
| 29 | | -export opaque type UninitializedModel = JSONValue; | | 29 | +export type UninitializedModel = JSONValue; | 30 | 30 |
| 31 | 31 | export type Response = ResponseBase; | 32 | 32 |
| |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/** | | 2 | + * Copyright (c) Facebook, Inc. and its affiliates. | | 3 | + * | | 4 | + * This source code is licensed under the MIT license found in the | | 5 | + * LICENSE file in the root directory of this source tree. | | 6 | + * | | 7 | + * @flow | | 8 | + */ | | 9 | + | | 10 | +import type {ModuleMetaData} from 'ReactFlightDOMRelayServerIntegration'; | | 11 | + | | 12 | +export type JSONValue = | | 13 | + | string | | 14 | + | number | | 15 | + | boolean | | 16 | + | null | | 17 | + | {+[key: string]: JSONValue} | | 18 | + | Array<JSONValue>; | | 19 | + | | 20 | +export type RowEncoding = | | 21 | + | ['J', number, JSONValue] | | 22 | + | ['M', number, ModuleMetaData] | | 23 | + | [ | | 24 | + 'E', | | 25 | + number, | | 26 | + { | | 27 | + message: string, | | 28 | + stack: string, | | 29 | + ... | | 30 | + }, | | 31 | + ]; |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-relay/src/ReactFlightDOMRelayServerHostConfig.js +12-53Lines changed: 12 additions & 53 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | * @flow | 8 | 8 | */ | 9 | 9 |
| | 10 | +import type {RowEncoding, JSONValue} from './ReactFlightDOMRelayProtocol'; | | 11 | + | 10 | 12 | import type {Request, ReactModel} from 'react-server/src/ReactFlightServer'; | 11 | 13 |
| 12 | 14 | import JSResourceReference from 'JSResourceReference'; | @@ -22,9 +24,7 @@ import type { | 22 | 24 | import {resolveModelToJSON} from 'react-server/src/ReactFlightServer'; | 23 | 25 |
| 24 | 26 | import { | 25 | | - emitModel, | 26 | | - emitModule, | 27 | | - emitError, | | 27 | + emitRow, | 28 | 28 | resolveModuleMetaData as resolveModuleMetaDataImpl, | 29 | 29 | } from 'ReactFlightDOMRelayServerIntegration'; | 30 | 30 |
| @@ -45,49 +45,22 @@ export function resolveModuleMetaData<T>( | 45 | 45 | return resolveModuleMetaDataImpl(config, resource); | 46 | 46 | } | 47 | 47 |
| 48 | | -type JSONValue = | 49 | | - | string | 50 | | - | number | 51 | | - | boolean | 52 | | - | null | 53 | | - | {+[key: string]: JSONValue} | 54 | | - | Array<JSONValue>; | 55 | | - | 56 | | -export type Chunk = | 57 | | - | { | 58 | | - type: 'json', | 59 | | - id: number, | 60 | | - json: JSONValue, | 61 | | - } | 62 | | - | { | 63 | | - type: 'module', | 64 | | - id: number, | 65 | | - json: ModuleMetaData, | 66 | | - } | 67 | | - | { | 68 | | - type: 'error', | 69 | | - id: number, | 70 | | - json: { | 71 | | - message: string, | 72 | | - stack: string, | 73 | | - ... | 74 | | - }, | 75 | | - }; | | 48 | +export type Chunk = RowEncoding; | 76 | 49 |
| 77 | 50 | export function processErrorChunk( | 78 | 51 | request: Request, | 79 | 52 | id: number, | 80 | 53 | message: string, | 81 | 54 | stack: string, | 82 | 55 | ): Chunk { | 83 | | - return { | 84 | | - type: 'error', | 85 | | - id: id, | 86 | | - json: { | | 56 | + return [ | | 57 | + 'E', | | 58 | + id, | | 59 | + { | 87 | 60 | message, | 88 | 61 | stack, | 89 | 62 | }, | 90 | | - }; | | 63 | + ]; | 91 | 64 | } | 92 | 65 |
| 93 | 66 | function convertModelToJSON( | @@ -126,11 +99,7 @@ export function processModelChunk( | 126 | 99 | model: ReactModel, | 127 | 100 | ): Chunk { | 128 | 101 | const json = convertModelToJSON(request, {}, '', model); | 129 | | - return { | 130 | | - type: 'json', | 131 | | - id: id, | 132 | | - json: json, | 133 | | - }; | | 102 | + return ['J', id, json]; | 134 | 103 | } | 135 | 104 |
| 136 | 105 | export function processModuleChunk( | @@ -139,11 +108,7 @@ export function processModuleChunk( | 139 | 108 | moduleMetaData: ModuleMetaData, | 140 | 109 | ): Chunk { | 141 | 110 | // The moduleMetaData is already a JSON serializable value. | 142 | | - return { | 143 | | - type: 'module', | 144 | | - id: id, | 145 | | - json: moduleMetaData, | 146 | | - }; | | 111 | + return ['M', id, moduleMetaData]; | 147 | 112 | } | 148 | 113 |
| 149 | 114 | export function scheduleWork(callback: () => void) { | @@ -155,13 +120,7 @@ export function flushBuffered(destination: Destination) {} | 155 | 120 | export function beginWriting(destination: Destination) {} | 156 | 121 |
| 157 | 122 | export function writeChunk(destination: Destination, chunk: Chunk): boolean { | 158 | | - if (chunk.type === 'json') { | 159 | | - emitModel(destination, chunk.id, chunk.json); | 160 | | - } else if (chunk.type === 'module') { | 161 | | - emitModule(destination, chunk.id, chunk.json); | 162 | | - } else { | 163 | | - emitError(destination, chunk.id, chunk.json.message, chunk.json.stack); | 164 | | - } | | 123 | + emitRow(destination, chunk); | 165 | 124 | return true; | 166 | 125 | } | 167 | 126 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-relay/src/__mocks__/ReactFlightDOMRelayServerIntegration.js +2-20Lines changed: 2 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | 'use strict'; | 9 | 9 |
| 10 | 10 | const ReactFlightDOMRelayServerIntegration = { | 11 | | - emitModel(destination, id, json) { | 12 | | - destination.push({ | 13 | | - type: 'json', | 14 | | - id: id, | 15 | | - json: json, | 16 | | - }); | 17 | | - }, | 18 | | - emitModule(destination, id, json) { | 19 | | - destination.push({ | 20 | | - type: 'module', | 21 | | - id: id, | 22 | | - json: json, | 23 | | - }); | 24 | | - }, | 25 | | - emitError(destination, id, message, stack) { | 26 | | - destination.push({ | 27 | | - type: 'error', | 28 | | - id: id, | 29 | | - json: {message, stack}, | 30 | | - }); | | 11 | + emitRow(destination, json) { | | 12 | + destination.push(json); | 31 | 13 | }, | 32 | 14 | close(destination) {}, | 33 | 15 | resolveModuleMetaData(config, resource) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-relay/src/__tests__/ReactFlightDOMRelay-test.internal.js +1-12Lines changed: 1 addition & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,18 +30,7 @@ describe('ReactFlightDOMRelay', () => { | 30 | 30 | const response = ReactDOMFlightRelayClient.createResponse(); | 31 | 31 | for (let i = 0; i < data.length; i++) { | 32 | 32 | const chunk = data[i]; | 33 | | - if (chunk.type === 'json') { | 34 | | - ReactDOMFlightRelayClient.resolveModel(response, chunk.id, chunk.json); | 35 | | - } else if (chunk.type === 'module') { | 36 | | - ReactDOMFlightRelayClient.resolveModule(response, chunk.id, chunk.json); | 37 | | - } else { | 38 | | - ReactDOMFlightRelayClient.resolveError( | 39 | | - response, | 40 | | - chunk.id, | 41 | | - chunk.json.message, | 42 | | - chunk.json.stack, | 43 | | - ); | 44 | | - } | | 33 | + ReactDOMFlightRelayClient.resolveRow(response, chunk); | 45 | 34 | } | 46 | 35 | ReactDOMFlightRelayClient.close(response); | 47 | 36 | const model = response.readRoot(); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-native-relay/src/ReactFlightNativeRelayClient.js +18-1Lines changed: 18 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | * @flow | 8 | 8 | */ | 9 | 9 |
| 10 | | -export { | | 10 | +import type {RowEncoding} from './ReactFlightNativeRelayProtocol'; | | 11 | + | | 12 | +import type {Response} from 'react-client/src/ReactFlightClient'; | | 13 | + | | 14 | +import { | 11 | 15 | createResponse, | 12 | 16 | resolveModel, | 13 | 17 | resolveModule, | 14 | 18 | resolveError, | 15 | 19 | close, | 16 | 20 | } from 'react-client/src/ReactFlightClient'; | | 21 | + | | 22 | +export {createResponse, close}; | | 23 | + | | 24 | +export function resolveRow(response: Response, chunk: RowEncoding): void { | | 25 | + if (chunk[0] === 'J') { | | 26 | + resolveModel(response, chunk[1], chunk[2]); | | 27 | + } else if (chunk[0] === 'M') { | | 28 | + resolveModule(response, chunk[1], chunk[2]); | | 29 | + } else { | | 30 | + // $FlowFixMe: Flow doesn't support disjoint unions on tuples. | | 31 | + resolveError(response, chunk[1], chunk[2].message, chunk[2].stack); | | 32 | + } | | 33 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-native-relay/src/ReactFlightNativeRelayClientHostConfig.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,7 +26,7 @@ export { | 26 | 26 |
| 27 | 27 | export type {ModuleMetaData} from 'ReactFlightNativeRelayClientIntegration'; | 28 | 28 |
| 29 | | -export opaque type UninitializedModel = JSONValue; | | 29 | +export type UninitializedModel = JSONValue; | 30 | 30 |
| 31 | 31 | export type Response = ResponseBase; | 32 | 32 |
| |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/** | | 2 | + * Copyright (c) Facebook, Inc. and its affiliates. | | 3 | + * | | 4 | + * This source code is licensed under the MIT license found in the | | 5 | + * LICENSE file in the root directory of this source tree. | | 6 | + * | | 7 | + * @flow | | 8 | + */ | | 9 | + | | 10 | +import type {ModuleMetaData} from 'ReactFlightNativeRelayServerIntegration'; | | 11 | + | | 12 | +export type JSONValue = | | 13 | + | string | | 14 | + | number | | 15 | + | boolean | | 16 | + | null | | 17 | + | {+[key: string]: JSONValue} | | 18 | + | Array<JSONValue>; | | 19 | + | | 20 | +export type RowEncoding = | | 21 | + | ['J', number, JSONValue] | | 22 | + | ['M', number, ModuleMetaData] | | 23 | + | [ | | 24 | + 'E', | | 25 | + number, | | 26 | + { | | 27 | + message: string, | | 28 | + stack: string, | | 29 | + ... | | 30 | + }, | | 31 | + ]; |
|