GeezerVison is showing github.com — [Flight] Add support for Module References in transport protocol (#20… · react/react@ffd8423
File treeExpand file treeCollapse file tree Open diff view settings react-transport-dom-relay/src react-transport-dom-webpack/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 +68-13Lines changed: 68 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,8 +41,9 @@ export type JSONValue = | 41 | 41 |
| 42 | 42 | const PENDING = 0; | 43 | 43 | const RESOLVED_MODEL = 1; | 44 | | -const INITIALIZED = 2; | 45 | | -const ERRORED = 3; | | 44 | +const RESOLVED_MODULE = 2; | | 45 | +const INITIALIZED = 3; | | 46 | +const ERRORED = 4; | 46 | 47 |
| 47 | 48 | type PendingChunk = { | 48 | 49 | _status: 0, | @@ -56,21 +57,28 @@ type ResolvedModelChunk = { | 56 | 57 | _response: Response, | 57 | 58 | then(resolve: () => mixed): void, | 58 | 59 | }; | 59 | | -type InitializedChunk<T> = { | | 60 | +type ResolvedModuleChunk<T> = { | 60 | 61 | _status: 2, | | 62 | + _value: ModuleReference<T>, | | 63 | + _response: Response, | | 64 | + then(resolve: () => mixed): void, | | 65 | +}; | | 66 | +type InitializedChunk<T> = { | | 67 | + _status: 3, | 61 | 68 | _value: T, | 62 | 69 | _response: Response, | 63 | 70 | then(resolve: () => mixed): void, | 64 | 71 | }; | 65 | 72 | type ErroredChunk = { | 66 | | - _status: 3, | | 73 | + _status: 4, | 67 | 74 | _value: Error, | 68 | 75 | _response: Response, | 69 | 76 | then(resolve: () => mixed): void, | 70 | 77 | }; | 71 | 78 | type SomeChunk<T> = | 72 | 79 | | PendingChunk | 73 | 80 | | ResolvedModelChunk | | 81 | + | ResolvedModuleChunk<T> | 74 | 82 | | InitializedChunk<T> | 75 | 83 | | ErroredChunk; | 76 | 84 |
| @@ -105,6 +113,8 @@ function readChunk<T>(chunk: SomeChunk<T>): T { | 105 | 113 | return chunk._value; | 106 | 114 | case RESOLVED_MODEL: | 107 | 115 | return initializeModelChunk(chunk); | | 116 | + case RESOLVED_MODULE: | | 117 | + return initializeModuleChunk(chunk); | 108 | 118 | case PENDING: | 109 | 119 | // eslint-disable-next-line no-throw-literal | 110 | 120 | throw (chunk: Wakeable); | @@ -155,6 +165,13 @@ function createResolvedModelChunk( | 155 | 165 | return new Chunk(RESOLVED_MODEL, value, response); | 156 | 166 | } | 157 | 167 |
| | 168 | +function createResolvedModuleChunk<T>( | | 169 | + response: Response, | | 170 | + value: ModuleReference<T>, | | 171 | +): ResolvedModuleChunk<T> { | | 172 | + return new Chunk(RESOLVED_MODULE, value, response); | | 173 | +} | | 174 | + | 158 | 175 | function resolveModelChunk<T>( | 159 | 176 | chunk: SomeChunk<T>, | 160 | 177 | value: UninitializedModel, | @@ -170,6 +187,21 @@ function resolveModelChunk<T>( | 170 | 187 | wakeChunk(listeners); | 171 | 188 | } | 172 | 189 |
| | 190 | +function resolveModuleChunk<T>( | | 191 | + chunk: SomeChunk<T>, | | 192 | + value: ModuleReference<T>, | | 193 | +): void { | | 194 | + if (chunk._status !== PENDING) { | | 195 | + // We already resolved. We didn't expect to see this. | | 196 | + return; | | 197 | + } | | 198 | + const listeners = chunk._value; | | 199 | + const resolvedChunk: ResolvedModuleChunk<T> = (chunk: any); | | 200 | + resolvedChunk._status = RESOLVED_MODULE; | | 201 | + resolvedChunk._value = value; | | 202 | + wakeChunk(listeners); | | 203 | +} | | 204 | + | 173 | 205 | function initializeModelChunk<T>(chunk: ResolvedModelChunk): T { | 174 | 206 | const value: T = parseModel(chunk._response, chunk._value); | 175 | 207 | const initializedChunk: InitializedChunk<T> = (chunk: any); | @@ -178,6 +210,14 @@ function initializeModelChunk<T>(chunk: ResolvedModelChunk): T { | 178 | 210 | return value; | 179 | 211 | } | 180 | 212 |
| | 213 | +function initializeModuleChunk<T>(chunk: ResolvedModuleChunk<T>): T { | | 214 | + const value: T = requireModule(chunk._value); | | 215 | + const initializedChunk: InitializedChunk<T> = (chunk: any); | | 216 | + initializedChunk._status = INITIALIZED; | | 217 | + initializedChunk._value = value; | | 218 | + return value; | | 219 | +} | | 220 | + | 181 | 221 | // Report that any missing chunks in the model is now going to throw this | 182 | 222 | // error upon read. Also notify any pending promises. | 183 | 223 | export function reportGlobalError(response: Response, error: Error): void { | @@ -241,7 +281,7 @@ function createElement(type, key, props): React$Element<any> { | 241 | 281 |
| 242 | 282 | type UninitializedBlockPayload<Data> = [ | 243 | 283 | mixed, | 244 | | - ModuleMetaData | SomeChunk<ModuleMetaData>, | | 284 | + BlockRenderFunction<any, Data> | SomeChunk<BlockRenderFunction<any, Data>>, | 245 | 285 | Data | SomeChunk<Data>, | 246 | 286 | Response, | 247 | 287 | ]; | @@ -250,14 +290,7 @@ function initializeBlock<Props, Data>( | 250 | 290 | tuple: UninitializedBlockPayload<Data>, | 251 | 291 | ): BlockComponent<Props, Data> { | 252 | 292 | // Require module first and then data. The ordering matters. | 253 | | - const moduleMetaData: ModuleMetaData = readMaybeChunk(tuple[1]); | 254 | | - const moduleReference: ModuleReference< | 255 | | - BlockRenderFunction<Props, Data>, | 256 | | - > = resolveModuleReference(moduleMetaData); | 257 | | - // TODO: Do this earlier, as the chunk is resolved. | 258 | | - preloadModule(moduleReference); | 259 | | - | 260 | | - const moduleExport = requireModule(moduleReference); | | 293 | + const moduleExport = readMaybeChunk(tuple[1]); | 261 | 294 |
| 262 | 295 | // The ordering here is important because this call might suspend. | 263 | 296 | // We don't want that to prevent the module graph for being initialized. | @@ -363,6 +396,28 @@ export function resolveModel( | 363 | 396 | } | 364 | 397 | } | 365 | 398 |
| | 399 | +export function resolveModule( | | 400 | + response: Response, | | 401 | + id: number, | | 402 | + model: UninitializedModel, | | 403 | +): void { | | 404 | + const chunks = response._chunks; | | 405 | + const chunk = chunks.get(id); | | 406 | + const moduleMetaData: ModuleMetaData = parseModel(response, model); | | 407 | + const moduleReference = resolveModuleReference(moduleMetaData); | | 408 | + | | 409 | + // TODO: Add an option to encode modules that are lazy loaded. | | 410 | + // For now we preload all modules as early as possible since it's likely | | 411 | + // that we'll need them. | | 412 | + preloadModule(moduleReference); | | 413 | + | | 414 | + if (!chunk) { | | 415 | + chunks.set(id, createResolvedModuleChunk(response, moduleReference)); | | 416 | + } else { | | 417 | + resolveModuleChunk(chunk, moduleReference); | | 418 | + } | | 419 | +} | | 420 | + | 366 | 421 | export function resolveError( | 367 | 422 | response: Response, | 368 | 423 | id: number, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-client/src/ReactFlightClientStream.js +11-3Lines changed: 11 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 | import type {Response} from './ReactFlightClientHostConfigStream'; | 11 | 11 |
| 12 | 12 | import { | | 13 | + resolveModule, | 13 | 14 | resolveModel, | 14 | 15 | resolveError, | 15 | 16 | createResponse as createResponseBase, | @@ -39,6 +40,13 @@ function processFullRow(response: Response, row: string): void { | 39 | 40 | resolveModel(response, id, json); | 40 | 41 | return; | 41 | 42 | } | | 43 | + 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); | | 48 | + return; | | 49 | + } | 42 | 50 | case 'E': { | 43 | 51 | const colon = row.indexOf(':', 1); | 44 | 52 | const id = parseInt(row.substring(1, colon), 16); | @@ -48,9 +56,9 @@ function processFullRow(response: Response, row: string): void { | 48 | 56 | return; | 49 | 57 | } | 50 | 58 | default: { | 51 | | - // Assume this is the root model. | 52 | | - resolveModel(response, 0, row); | 53 | | - return; | | 59 | + throw new Error( | | 60 | + "Error parsing the data. It's probably an error code or network corruption.", | | 61 | + ); | 54 | 62 | } | 55 | 63 | } | 56 | 64 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-client/src/__tests__/ReactFlight-test.js +43-2Lines changed: 43 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,17 +53,29 @@ describe('ReactFlight', () => { | 53 | 53 | }; | 54 | 54 | }); | 55 | 55 |
| | 56 | + function moduleReference(value) { | | 57 | + return { | | 58 | + $$typeof: Symbol.for('react.module.reference'), | | 59 | + value: value, | | 60 | + }; | | 61 | + } | | 62 | + | 56 | 63 | function block(render, load) { | 57 | 64 | if (load === undefined) { | 58 | 65 | return () => { | 59 | | - return ReactNoopFlightServerRuntime.serverBlockNoData(render); | | 66 | + return ReactNoopFlightServerRuntime.serverBlockNoData( | | 67 | + moduleReference(render), | | 68 | + ); | 60 | 69 | }; | 61 | 70 | } | 62 | 71 | return function(...args) { | 63 | 72 | const curriedLoad = () => { | 64 | 73 | return load(...args); | 65 | 74 | }; | 66 | | - return ReactNoopFlightServerRuntime.serverBlock(render, curriedLoad); | | 75 | + return ReactNoopFlightServerRuntime.serverBlock( | | 76 | + moduleReference(render), | | 77 | + curriedLoad, | | 78 | + ); | 67 | 79 | }; | 68 | 80 | } | 69 | 81 |
| @@ -97,6 +109,35 @@ describe('ReactFlight', () => { | 97 | 109 | }); | 98 | 110 | }); | 99 | 111 |
| | 112 | + it('can render a client component using a module reference and render there', () => { | | 113 | + function UserClient(props) { | | 114 | + return ( | | 115 | + <span> | | 116 | + {props.greeting}, {props.name} | | 117 | + </span> | | 118 | + ); | | 119 | + } | | 120 | + const User = moduleReference(UserClient); | | 121 | + | | 122 | + function Greeting({firstName, lastName}) { | | 123 | + return <User greeting="Hello" name={firstName + ' ' + lastName} />; | | 124 | + } | | 125 | + | | 126 | + const model = { | | 127 | + greeting: <Greeting firstName="Seb" lastName="Smith" />, | | 128 | + }; | | 129 | + | | 130 | + const transport = ReactNoopFlightServer.render(model); | | 131 | + | | 132 | + act(() => { | | 133 | + const rootModel = ReactNoopFlightClient.read(transport); | | 134 | + const greeting = rootModel.greeting; | | 135 | + ReactNoop.render(greeting); | | 136 | + }); | | 137 | + | | 138 | + expect(ReactNoop).toMatchRenderedOutput(<span>Hello, Seb Smith</span>); | | 139 | + }); | | 140 | + | 100 | 141 | if (ReactFeatureFlags.enableBlocksAPI) { | 101 | 142 | it('can transfer a Block to the client and render there, without data', () => { | 102 | 143 | function User(props, data) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-noop-renderer/src/ReactNoopFlightServer.js +8-2Lines changed: 8 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,8 +42,14 @@ const ReactNoopFlightServer = ReactFlightServer({ | 42 | 42 | formatChunk(type: string, props: Object): Uint8Array { | 43 | 43 | return Buffer.from(JSON.stringify({type, props}), 'utf8'); | 44 | 44 | }, | 45 | | - resolveModuleMetaData(config: void, renderFn: Function) { | 46 | | - return saveModule(renderFn); | | 45 | + isModuleReference(reference: Object): boolean { | | 46 | + return reference.$$typeof === Symbol.for('react.module.reference'); | | 47 | + }, | | 48 | + resolveModuleMetaData( | | 49 | + config: void, | | 50 | + reference: {$$typeof: Symbol, value: any}, | | 51 | + ) { | | 52 | + return saveModule(reference.value); | 47 | 53 | }, | 48 | 54 | }); | 49 | 55 |
| |
|