-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdefer.tsx
More file actions
192 lines (170 loc) · 5.24 KB
/
defer.tsx
File metadata and controls
192 lines (170 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import type { ReactElement, ReactNode } from "react";
import { renderToReadableStream } from "@vitejs/plugin-rsc/react/rsc";
import { DeferredComponent } from "#rsc-client";
import { drainStream } from "../util/drainStream";
import { getPayloadIDFor } from "./rscModule";
import { rscPayloadDir } from "virtual:funstack/config";
export interface DeferEntry {
state: DeferEntryState;
name?: string;
drainPromise?: Promise<string>;
}
/**
* Options for the defer function.
*/
export interface DeferOptions {
/**
* Optional name for debugging purposes.
* In development: included in the RSC payload file name.
* In production: logged when the payload file is emitted.
*/
name?: string;
}
export interface LoadedDeferEntry extends DeferEntry {
state: Exclude<DeferEntryState, { state: "pending" }>;
drainPromise: Promise<string>;
}
type DeferEntryState =
| {
state: "pending";
element: ReactElement;
}
| {
state: "streaming";
stream: ReadableStream<Uint8Array>;
}
| {
state: "ready";
}
| {
state: "error";
error: unknown;
};
/**
* Sanitizes a name for use in file paths.
* Replaces non-alphanumeric characters with underscores and limits length.
*/
function sanitizeName(name: string): string {
return name
.replace(/[^a-zA-Z0-9_-]/g, "_")
.replace(/_+/g, "_")
.replace(/^_|_$/g, "")
.slice(0, 50);
}
export class DeferRegistry {
#registry = new Map<string, DeferEntry>();
register(element: ReactElement, id: string, name?: string) {
this.#registry.set(id, { state: { element, state: "pending" }, name });
}
load(id: string): LoadedDeferEntry | undefined {
const entry = this.#registry.get(id);
if (!entry) {
return undefined;
}
return this.#loadEntry(entry);
}
#loadEntry(entry: DeferEntry): LoadedDeferEntry {
const { state } = entry;
switch (state.state) {
case "pending": {
const stream = renderToReadableStream<ReactNode>(state.element);
const [stream1, stream2] = stream.tee();
entry.state = { state: "streaming", stream: stream1 };
const drainPromise = drainStream(stream2);
entry.drainPromise = drainPromise;
drainPromise.then(
() => {
entry.state = { state: "ready" };
},
(error) => {
entry.state = { state: "error", error };
},
);
return entry as LoadedDeferEntry;
}
case "streaming":
case "ready":
case "error":
return entry as LoadedDeferEntry;
}
}
has(id: string): boolean {
return this.#registry.has(id);
}
/**
* Iterates over all entries in parallel.
* Yields results as each stream completes.
*/
async *loadAll() {
const errors: unknown[] = [];
// Phase 1: Start all entries loading and collect drain promises.
// We use drain promises (which drain stream2 from tee) instead of
// draining stream1 directly, because stream1 may have been locked
// by createFromReadableStream during SSR.
const loadedEntries = Array.from(this.#registry, ([id, entry]) => {
const loaded = this.#loadEntry(entry);
return [id, loaded.drainPromise, entry.name] as const;
});
if (loadedEntries.length === 0) return;
type Result = { id: string; data: string; name?: string };
// Completion queue
const completed: Array<Result | { error: unknown }> = [];
let waiting: (() => void) | undefined;
let remainingCount = loadedEntries.length;
const onComplete = (result: Result | { error: unknown }) => {
completed.push(result);
remainingCount--;
waiting?.();
};
// Phase 2: Await drain promises
for (const [id, drainPromise, name] of loadedEntries) {
drainPromise.then(
(data) => onComplete({ id, data, name }),
(error) => onComplete({ error }),
);
}
// Phase 3: Yield from queue as results arrive
while (remainingCount > 0 || completed.length > 0) {
if (completed.length === 0) {
await new Promise<void>((r) => {
waiting = r;
});
waiting = undefined;
}
for (const result of completed.splice(0)) {
if ("error" in result) {
errors.push(result.error);
} else {
yield result;
}
}
}
if (errors.length > 0) {
throw new AggregateError(errors);
}
}
}
export const deferRegistry = new DeferRegistry();
/**
* Renders given Server Component into a separate RSC payload.
*
* During the client side rendering, fetching of the payload will be
* deferred until the returned ReactNode is actually rendered.
*
* @param element - The React element to defer.
* @param options - Optional configuration for the deferred payload.
* @returns A ReactNode that virtually contains the result of rendering the given component.
*/
export function defer(
element: ReactElement,
options?: DeferOptions,
): ReactNode {
const name = options?.name;
const sanitizedName = name ? sanitizeName(name) : undefined;
const rawId = sanitizedName
? `${sanitizedName}-${crypto.randomUUID()}`
: crypto.randomUUID();
const id = getPayloadIDFor(rawId, rscPayloadDir);
deferRegistry.register(element, id, name);
return <DeferredComponent moduleID={id} />;
}