Skip to content

Commit

Permalink
Emit blob without extra indirection
Browse files Browse the repository at this point in the history
Now that we can have future references for blobs we don't need an extra
indirection reference and can treat it the same as promises.
  • Loading branch information
sebmarkbage committed Apr 8, 2024
1 parent c1dc05b commit 3eb87e6
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1239,43 +1239,42 @@ function serializeTypedArray(
}

function serializeBlob(request: Request, blob: Blob): string {
const id = request.nextChunkId++;
request.pendingChunks++;
const model: Array<string | Uint8Array> = [blob.type];
const newTask = createTask(
request,
model,
null,
false,
request.abortableTasks,
);

const reader = blob.stream().getReader();

const model: Array<string | Uint8Array> = [blob.type];

function progress(
entry: {done: false, value: Uint8Array} | {done: true, value: void},
): Promise<void> | void {
if (entry.done) {
const blobId = outlineModel(request, model);
const blobReference = '$B' + blobId.toString(16);
const processedChunk = encodeReferenceChunk(request, id, blobReference);
request.completedRegularChunks.push(processedChunk);
if (request.destination !== null) {
flushCompletedChunks(request, request.destination);
}
pingTask(request, newTask);
return;
}
// TODO: Emit the chunk early and refer to it later.
// TODO: Emit the chunk early and refer to it later by dedupe.
model.push(entry.value);
// $FlowFixMe[incompatible-call]
return reader.read().then(progress).catch(error);
}

function error(reason: mixed) {
const digest = logRecoverableError(request, reason);
emitErrorChunk(request, id, digest, reason);
emitErrorChunk(request, newTask.id, digest, reason);
request.abortableTasks.delete(newTask);
if (request.destination !== null) {
flushCompletedChunks(request, request.destination);
}
}
// $FlowFixMe[incompatible-call]
reader.read().then(progress).catch(error);

return '$' + id.toString(16);
return '$B' + newTask.id.toString(16);
}

function escapeStringValue(value: string): string {
Expand Down

0 comments on commit 3eb87e6

Please sign in to comment.