Skip to content

Commit

Permalink
Encode Relay rows as tuples instead of objects
Browse files Browse the repository at this point in the history
This is slightly more compact and more ressembles more closely the encoding
we use for the raw stream protocol.
  • Loading branch information
sebmarkbage committed Nov 5, 2020
1 parent 914e279 commit 69e49b4
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import {
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);
if (chunk[0] === 'J') {
resolveModel(response, chunk[1], chunk[2]);
} else if (chunk[0] === 'M') {
resolveModule(response, chunk[1], chunk[2]);
} else if (chunk[0] === 'E') {
// $FlowFixMe: Flow doesn't support disjoint unions on tuples.
resolveError(response, chunk[1], chunk[2].message, chunk[2].stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,14 @@ export type JSONValue =
| Array<JSONValue>;

export type RowEncoding =
| {
type: 'json',
id: number,
json: JSONValue,
}
| {
type: 'module',
id: number,
json: ModuleMetaData,
}
| {
type: 'error',
id: number,
json: {
| ['J', number, JSONValue]
| ['M', number, ModuleMetaData]
| [
'E',
number,
{
message: string,
stack: string,
...
},
};
];
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ export function processErrorChunk(
message: string,
stack: string,
): Chunk {
return {
type: 'error',
id: id,
json: {
return [
'E',
id,
{
message,
stack,
},
};
];
}

function convertModelToJSON(
Expand Down Expand Up @@ -99,11 +99,7 @@ export function processModelChunk(
model: ReactModel,
): Chunk {
const json = convertModelToJSON(request, {}, '', model);
return {
type: 'json',
id: id,
json: json,
};
return ['J', id, json];
}

export function processModuleChunk(
Expand All @@ -112,11 +108,7 @@ export function processModuleChunk(
moduleMetaData: ModuleMetaData,
): Chunk {
// The moduleMetaData is already a JSON serializable value.
return {
type: 'module',
id: id,
json: moduleMetaData,
};
return ['M', id, moduleMetaData];
}

export function scheduleWork(callback: () => void) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import {
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);
if (chunk[0] === 'J') {
resolveModel(response, chunk[1], chunk[2]);
} else if (chunk[0] === 'M') {
resolveModule(response, chunk[1], chunk[2]);
} else if (chunk[0] === 'E') {
// $FlowFixMe: Flow doesn't support disjoint unions on tuples.
resolveError(response, chunk[1], chunk[2].message, chunk[2].stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,14 @@ export type JSONValue =
| Array<JSONValue>;

export type RowEncoding =
| {
type: 'json',
id: number,
json: JSONValue,
}
| {
type: 'module',
id: number,
json: ModuleMetaData,
}
| {
type: 'error',
id: number,
json: {
| ['J', number, JSONValue]
| ['M', number, ModuleMetaData]
| [
'E',
number,
{
message: string,
stack: string,
...
},
};
];
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ export function processErrorChunk(
message: string,
stack: string,
): Chunk {
return {
type: 'error',
id: id,
json: {
return [
'E',
id,
{
message,
stack,
},
};
];
}

function convertModelToJSON(
Expand Down Expand Up @@ -99,11 +99,7 @@ export function processModelChunk(
model: ReactModel,
): Chunk {
const json = convertModelToJSON(request, {}, '', model);
return {
type: 'json',
id: id,
json: json,
};
return ['J', id, json];
}

export function processModuleChunk(
Expand All @@ -112,11 +108,7 @@ export function processModuleChunk(
moduleMetaData: ModuleMetaData,
): Chunk {
// The moduleMetaData is already a JSON serializable value.
return {
type: 'module',
id: id,
json: moduleMetaData,
};
return ['M', id, moduleMetaData];
}

export function scheduleWork(callback: () => void) {
Expand Down

0 comments on commit 69e49b4

Please sign in to comment.