forked from kitajs/html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suspense.d.ts
145 lines (133 loc) · 4.41 KB
/
suspense.d.ts
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
import type { Readable } from 'stream';
import type { Children } from './';
declare global {
/**
* The `SUSPENSE_ROOT` is a global object that holds the state of all the suspense
* components rendered in the server.
*/
var SUSPENSE_ROOT: {
/**
* The suspense resource map is a map of all request ids and its resources containing
* the stream to write the HTML, the number of running promises and if the first
* suspense has already resolved.
*/
resources: Map<
number,
{
/** If the first suspense has already resolved */
sent: boolean;
/** How many are still running */
running: number;
/** The stream we should write */
stream: WeakRef<Readable>;
}
>;
/**
* This value is used (and incremented shortly after) when no requestId is provided
* for {@linkcode renderToStream}
*
* @default 1
*/
requestCounter: number;
/**
* If the usage of suspense is enabled.
*
* @default false
*/
enabled: boolean;
/**
* If we should automatically stream {@linkcode SuspenseScript} before the first
* suspense is rendered. If you disable this setting, you need to manually load the
* {@linkcode SuspenseScript} in your HTML before any suspense is rendered. Otherwise,
* the suspense will not work.
*
* @default true
*/
autoScript: boolean;
};
}
/**
* A component that returns a fallback while the async children are loading.
*
* The `rid` prop is the one {@linkcode renderToStream} returns, this way the suspense
* knows which request it belongs to.
*/
export function Suspense(props: SuspenseProps): JSX.Element;
/**
* Transforms a component tree who may contain `Suspense` components into a stream of
* HTML.
*
* @example ;```tsx // Simple nodejs webserver to render html http.createServer((req, res)
* => { res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
*
* const stream = renderToStream(r => <AppWithSuspense rid={r} />)
*
* stream.pipe(res)
*
* }).listen(1227)
*
* // Prints out the rendered stream const stream = renderToStream(r => <AppWithSuspense
* rid={r} />)
*
* For await (const html of stream) { console.log(html.toString()) }
*
* @param factory The component tree to render. @param rid The resource id to identify the
* request, if not provided, a new incrementing id will be used. @see {@linkcode Suspense}
*/
export function renderToStream(
factory: (this: void, rid: number) => JSX.Element,
rid?: number
): Readable & {
/** The final resource id used for this render */
rid: number;
};
/**
* Returns a promise that resolves to the entire HTML generated by the component tree.
* **Suspense calls are waited for**.
*
* This method is a shorthand to call {@linkcode renderToStream} and collect its result
* into a string.
*
* **Rendering to string will not give any benefits over streaming, it will only be
* slower.**
*
* @example ;```tsx // Does not uses suspense benefits! Useful for testing. Prefer to //
* use renderToStream instead. const html = await renderToString(r => <AppWithSuspense
* rid={r} />) console.log(html)
*
* @param factory The component tree to render. @param rid The resource id to identify the
* request, if not provided, a new incrementing id will be used. @see
* {@linkcode renderToStream}
*/
export function renderToString(
factory: (this: void, rid: number) => JSX.Element,
rid?: number
): Promise<string>;
/**
* This script needs to be loaded at the top of the page. You do not need to load it
* manually, unless GLOBAL_SUSPENSE.autoScript is set to false.
*
* @see {@linkcode Suspense}
*/
export const SuspenseScript: string;
/**
* The props for the `Suspense` component.
*
* @see {@linkcode Suspense}
*/
export interface SuspenseProps {
/** The resource id is used to identify the request for this suspense. */
rid: number;
/** The fallback to render while the async children are loading. */
fallback: JSX.Element;
/** The async children to render as soon as they are ready. */
children: Children;
/**
* This error boundary is used to catch any error thrown by an async component and
* streams its fallback instead.
*
* This does not catches for errors thrown by the suspense itself or async fallback
* components. Please use {@linkcode ErrorBoundary} to catch them instead.
*/
catch?: JSX.Element | ((error: unknown) => JSX.Element);
}