Skip to content

Commit 94d2767

Browse files
committed
Encode Relay rows as tuples instead of objects
This is slightly more compact and more ressembles more closely the encoding we use for the raw stream protocol.
1 parent 914e279 commit 94d2767

6 files changed

+40
-70
lines changed

packages/react-transport-dom-relay/src/ReactFlightDOMRelayClient.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import {
2222
export {createResponse, close};
2323

2424
export function resolveRow(response: Response, chunk: RowEncoding): void {
25-
if (chunk.type === 'json') {
26-
resolveModel(response, chunk.id, chunk.json);
27-
} else if (chunk.type === 'module') {
28-
resolveModule(response, chunk.id, chunk.json);
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]);
2929
} else {
30-
resolveError(response, chunk.id, chunk.json.message, chunk.json.stack);
30+
// $FlowFixMe: Flow doesn't support disjoint unions on tuples.
31+
resolveError(response, chunk[1], chunk[2].message, chunk[2].stack);
3132
}
3233
}

packages/react-transport-dom-relay/src/ReactFlightDOMRelayProtocol.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,14 @@ export type JSONValue =
1818
| Array<JSONValue>;
1919

2020
export type RowEncoding =
21-
| {
22-
type: 'json',
23-
id: number,
24-
json: JSONValue,
25-
}
26-
| {
27-
type: 'module',
28-
id: number,
29-
json: ModuleMetaData,
30-
}
31-
| {
32-
type: 'error',
33-
id: number,
34-
json: {
21+
| ['J', number, JSONValue]
22+
| ['M', number, ModuleMetaData]
23+
| [
24+
'E',
25+
number,
26+
{
3527
message: string,
3628
stack: string,
3729
...
3830
},
39-
};
31+
];

packages/react-transport-dom-relay/src/ReactFlightDOMRelayServerHostConfig.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ export function processErrorChunk(
5353
message: string,
5454
stack: string,
5555
): Chunk {
56-
return {
57-
type: 'error',
58-
id: id,
59-
json: {
56+
return [
57+
'E',
58+
id,
59+
{
6060
message,
6161
stack,
6262
},
63-
};
63+
];
6464
}
6565

6666
function convertModelToJSON(
@@ -99,11 +99,7 @@ export function processModelChunk(
9999
model: ReactModel,
100100
): Chunk {
101101
const json = convertModelToJSON(request, {}, '', model);
102-
return {
103-
type: 'json',
104-
id: id,
105-
json: json,
106-
};
102+
return ['J', id, json];
107103
}
108104

109105
export function processModuleChunk(
@@ -112,11 +108,7 @@ export function processModuleChunk(
112108
moduleMetaData: ModuleMetaData,
113109
): Chunk {
114110
// The moduleMetaData is already a JSON serializable value.
115-
return {
116-
type: 'module',
117-
id: id,
118-
json: moduleMetaData,
119-
};
111+
return ['M', id, moduleMetaData];
120112
}
121113

122114
export function scheduleWork(callback: () => void) {

packages/react-transport-native-relay/src/ReactFlightNativeRelayClient.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import {
2222
export {createResponse, close};
2323

2424
export function resolveRow(response: Response, chunk: RowEncoding): void {
25-
if (chunk.type === 'json') {
26-
resolveModel(response, chunk.id, chunk.json);
27-
} else if (chunk.type === 'module') {
28-
resolveModule(response, chunk.id, chunk.json);
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]);
2929
} else {
30-
resolveError(response, chunk.id, chunk.json.message, chunk.json.stack);
30+
// $FlowFixMe: Flow doesn't support disjoint unions on tuples.
31+
resolveError(response, chunk[1], chunk[2].message, chunk[2].stack);
3132
}
3233
}

packages/react-transport-native-relay/src/ReactFlightNativeRelayProtocol.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,14 @@ export type JSONValue =
1818
| Array<JSONValue>;
1919

2020
export type RowEncoding =
21-
| {
22-
type: 'json',
23-
id: number,
24-
json: JSONValue,
25-
}
26-
| {
27-
type: 'module',
28-
id: number,
29-
json: ModuleMetaData,
30-
}
31-
| {
32-
type: 'error',
33-
id: number,
34-
json: {
21+
| ['J', number, JSONValue]
22+
| ['M', number, ModuleMetaData]
23+
| [
24+
'E',
25+
number,
26+
{
3527
message: string,
3628
stack: string,
3729
...
3830
},
39-
};
31+
];

packages/react-transport-native-relay/src/ReactFlightNativeRelayServerHostConfig.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ export function processErrorChunk(
5353
message: string,
5454
stack: string,
5555
): Chunk {
56-
return {
57-
type: 'error',
58-
id: id,
59-
json: {
56+
return [
57+
'E',
58+
id,
59+
{
6060
message,
6161
stack,
6262
},
63-
};
63+
];
6464
}
6565

6666
function convertModelToJSON(
@@ -99,11 +99,7 @@ export function processModelChunk(
9999
model: ReactModel,
100100
): Chunk {
101101
const json = convertModelToJSON(request, {}, '', model);
102-
return {
103-
type: 'json',
104-
id: id,
105-
json: json,
106-
};
102+
return ['J', id, json];
107103
}
108104

109105
export function processModuleChunk(
@@ -112,11 +108,7 @@ export function processModuleChunk(
112108
moduleMetaData: ModuleMetaData,
113109
): Chunk {
114110
// The moduleMetaData is already a JSON serializable value.
115-
return {
116-
type: 'module',
117-
id: id,
118-
json: moduleMetaData,
119-
};
111+
return ['M', id, moduleMetaData];
120112
}
121113

122114
export function scheduleWork(callback: () => void) {

0 commit comments

Comments
 (0)