React DOM legacy server stream config
作用
导出类型
目的地
// 目的地
export interface Destination {
push(chunk: string | null): boolean;
destroy(error: Error): mixed;
}
预计算块
// 预计算块
export opaque type PrecomputedChunk = string;
块
// 块
export opaque type Chunk = string;
二进制块
// 二进制块
export opaque type BinaryChunk = string;
导出常量
块的字节长度
备注
源码中 69 - 71 行
export const byteLengthOfChunk:
| null
| ((chunk: Chunk | PrecomputedChunk) => number) = null;
安排工作
export function scheduleWork(callback: () => void) {
callback();
}
安排微任务
export function scheduleMicrotask(callback: () => void) {
// While this defies the method name the legacy builds have special
// overrides that make work scheduling sync. At the moment scheduleMicrotask
// isn't used by any legacy APIs so this is somewhat academic but if they
// did in the future we'd probably want to have this be in sync with scheduleWork
// 虽然这违背了方法名称,但旧版本的构建有特殊的覆盖,使工作调度同步。目前没有任何旧版
// API 使用 scheduleMicrotask,所以这在某种程度上是学术性的,但如果将来它们使用,我们
// 可能希望它与 scheduleWork 保持同步
callback();
}
刷新缓冲
export function flushBuffered(destination: Destination) {}
开始写作
export function beginWriting(destination: Destination) {}
写块
export function writeChunk(
destination: Destination,
chunk: Chunk | PrecomputedChunk | BinaryChunk,
): void {
writeChunkAndReturn(destination, chunk);
}
写入块并返回
export function writeChunkAndReturn(
destination: Destination,
chunk: Chunk | PrecomputedChunk | BinaryChunk,
): boolean {
return destination.push(chunk);
}
完成写作
export function completeWriting(destination: Destination) {}
关闭
export function close(destination: Destination) {
destination.push(null);
}
字符串到块
export function stringToChunk(content: string): Chunk {
return content;
}
字符串到预计算块
export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
return content;
}
将类型化数组转换为二进制块
export function typedArrayToBinaryChunk(
content: $ArrayBufferView,
): BinaryChunk {
throw new Error('Not implemented.');
}
二进制块的字节长度
export function byteLengthOfBinaryChunk(chunk: BinaryChunk): number {
throw new Error('Not implemented.');
}
关闭并出错
export function closeWithError(destination: Destination, error: mixed): void {
destination.destroy(error);
}
创建快速哈希 JS
备注
createFastHash()由 react-server 实现
export { createFastHashJS as createFastHash } from 'react-server/src/createFastHashJS';
读取为数据 URL
export function readAsDataURL(blob: Blob): Promise<string> {
return blob.arrayBuffer().then(arrayBuffer => {
const encoded =
typeof Buffer === 'function' && typeof Buffer.from === 'function'
? Buffer.from(arrayBuffer).toString('base64')
: btoa(String.fromCharCode.apply(String, new Uint8Array(arrayBuffer)));
const mimeType = blob.type || 'application/octet-stream';
return 'data:' + mimeType + ';base64,' + encoded;
});
}