Skip to content

Commit

Permalink
Simplify Relay protocol integration
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Nov 5, 2020
1 parent 6b28eb6 commit 914e279
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@
* @flow
*/

export {
import type {RowEncoding} from './ReactFlightDOMRelayProtocol';

import type {Response} from 'react-client/src/ReactFlightClient';

import {
createResponse,
resolveModel,
resolveModule,
resolveError,
close,
} from 'react-client/src/ReactFlightClient';

export {createResponse, close};

export function resolveRow(response: Response, chunk: RowEncoding): void {
if (chunk.type === 'json') {
resolveModel(response, chunk.id, chunk.json);
} else if (chunk.type === 'module') {
resolveModule(response, chunk.id, chunk.json);
} else {
resolveError(response, chunk.id, chunk.json.message, chunk.json.stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export {

export type {ModuleMetaData} from 'ReactFlightDOMRelayClientIntegration';

export opaque type UninitializedModel = JSONValue;
export type UninitializedModel = JSONValue;

export type Response = ResponseBase;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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 {ModuleMetaData} from 'ReactFlightDOMRelayServerIntegration';

export type JSONValue =
| string
| number
| boolean
| null
| {+[key: string]: JSONValue}
| Array<JSONValue>;

export type RowEncoding =
| {
type: 'json',
id: number,
json: JSONValue,
}
| {
type: 'module',
id: number,
json: ModuleMetaData,
}
| {
type: 'error',
id: number,
json: {
message: string,
stack: string,
...
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @flow
*/

import type {RowEncoding, JSONValue} from './ReactFlightDOMRelayProtocol';

import type {Request, ReactModel} from 'react-server/src/ReactFlightServer';

import JSResourceReference from 'JSResourceReference';
Expand All @@ -22,9 +24,7 @@ import type {
import {resolveModelToJSON} from 'react-server/src/ReactFlightServer';

import {
emitModel,
emitModule,
emitError,
emitRow,
resolveModuleMetaData as resolveModuleMetaDataImpl,
} from 'ReactFlightDOMRelayServerIntegration';

Expand All @@ -45,34 +45,7 @@ export function resolveModuleMetaData<T>(
return resolveModuleMetaDataImpl(config, resource);
}

type JSONValue =
| string
| number
| boolean
| null
| {+[key: string]: JSONValue}
| Array<JSONValue>;

export type Chunk =
| {
type: 'json',
id: number,
json: JSONValue,
}
| {
type: 'module',
id: number,
json: ModuleMetaData,
}
| {
type: 'error',
id: number,
json: {
message: string,
stack: string,
...
},
};
export type Chunk = RowEncoding;

export function processErrorChunk(
request: Request,
Expand Down Expand Up @@ -155,13 +128,7 @@ export function flushBuffered(destination: Destination) {}
export function beginWriting(destination: Destination) {}

export function writeChunk(destination: Destination, chunk: Chunk): boolean {
if (chunk.type === 'json') {
emitModel(destination, chunk.id, chunk.json);
} else if (chunk.type === 'module') {
emitModule(destination, chunk.id, chunk.json);
} else {
emitError(destination, chunk.id, chunk.json.message, chunk.json.stack);
}
emitRow(destination, chunk);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,8 @@
'use strict';

const ReactFlightDOMRelayServerIntegration = {
emitModel(destination, id, json) {
destination.push({
type: 'json',
id: id,
json: json,
});
},
emitModule(destination, id, json) {
destination.push({
type: 'module',
id: id,
json: json,
});
},
emitError(destination, id, message, stack) {
destination.push({
type: 'error',
id: id,
json: {message, stack},
});
emitRow(destination, json) {
destination.push(json);
},
close(destination) {},
resolveModuleMetaData(config, resource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,7 @@ describe('ReactFlightDOMRelay', () => {
const response = ReactDOMFlightRelayClient.createResponse();
for (let i = 0; i < data.length; i++) {
const chunk = data[i];
if (chunk.type === 'json') {
ReactDOMFlightRelayClient.resolveModel(response, chunk.id, chunk.json);
} else if (chunk.type === 'module') {
ReactDOMFlightRelayClient.resolveModule(response, chunk.id, chunk.json);
} else {
ReactDOMFlightRelayClient.resolveError(
response,
chunk.id,
chunk.json.message,
chunk.json.stack,
);
}
ReactDOMFlightRelayClient.resolveRow(response, chunk);
}
ReactDOMFlightRelayClient.close(response);
const model = response.readRoot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@
* @flow
*/

export {
import type {RowEncoding} from './ReactFlightNativeRelayProtocol';

import type {Response} from 'react-client/src/ReactFlightClient';

import {
createResponse,
resolveModel,
resolveModule,
resolveError,
close,
} from 'react-client/src/ReactFlightClient';

export {createResponse, close};

export function resolveRow(response: Response, chunk: RowEncoding): void {
if (chunk.type === 'json') {
resolveModel(response, chunk.id, chunk.json);
} else if (chunk.type === 'module') {
resolveModule(response, chunk.id, chunk.json);
} else {
resolveError(response, chunk.id, chunk.json.message, chunk.json.stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export {

export type {ModuleMetaData} from 'ReactFlightNativeRelayClientIntegration';

export opaque type UninitializedModel = JSONValue;
export type UninitializedModel = JSONValue;

export type Response = ResponseBase;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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 {ModuleMetaData} from 'ReactFlightNativeRelayServerIntegration';

export type JSONValue =
| string
| number
| boolean
| null
| {+[key: string]: JSONValue}
| Array<JSONValue>;

export type RowEncoding =
| {
type: 'json',
id: number,
json: JSONValue,
}
| {
type: 'module',
id: number,
json: ModuleMetaData,
}
| {
type: 'error',
id: number,
json: {
message: string,
stack: string,
...
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @flow
*/

import type {RowEncoding, JSONValue} from './ReactFlightNativeRelayProtocol';

import type {Request, ReactModel} from 'react-server/src/ReactFlightServer';

import JSResourceReferenceImpl from 'JSResourceReferenceImpl';
Expand All @@ -22,9 +24,7 @@ import type {
import {resolveModelToJSON} from 'react-server/src/ReactFlightServer';

import {
emitModel,
emitModule,
emitError,
emitRow,
resolveModuleMetaData as resolveModuleMetaDataImpl,
} from 'ReactFlightNativeRelayServerIntegration';

Expand All @@ -45,34 +45,7 @@ export function resolveModuleMetaData<T>(
return resolveModuleMetaDataImpl(config, resource);
}

type JSONValue =
| string
| number
| boolean
| null
| {+[key: string]: JSONValue}
| Array<JSONValue>;

export type Chunk =
| {
type: 'json',
id: number,
json: JSONValue,
}
| {
type: 'module',
id: number,
json: ModuleMetaData,
}
| {
type: 'error',
id: number,
json: {
message: string,
stack: string,
...
},
};
export type Chunk = RowEncoding;

export function processErrorChunk(
request: Request,
Expand Down Expand Up @@ -155,13 +128,7 @@ export function flushBuffered(destination: Destination) {}
export function beginWriting(destination: Destination) {}

export function writeChunk(destination: Destination, chunk: Chunk): boolean {
if (chunk.type === 'json') {
emitModel(destination, chunk.id, chunk.json);
} else if (chunk.type === 'module') {
emitModule(destination, chunk.id, chunk.json);
} else {
emitError(destination, chunk.id, chunk.json.message, chunk.json.stack);
}
emitRow(destination, chunk);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,8 @@
'use strict';

const ReactFlightNativeRelayServerIntegration = {
emitModel(destination, id, json) {
destination.push({
type: 'json',
id: id,
json: json,
});
},
emitModule(destination, id, json) {
destination.push({
type: 'module',
id: id,
json: json,
});
},
emitError(destination, id, message, stack) {
destination.push({
type: 'error',
id: id,
json: {message, stack},
});
emitRow(destination, json) {
destination.push(json);
},
close(destination) {},
resolveModuleMetaData(config, resource) {
Expand Down
Loading

0 comments on commit 914e279

Please sign in to comment.