@b9g/crank 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/_utils.d.ts +14 -0
- package/async.cjs +237 -0
- package/async.cjs.map +1 -0
- package/async.d.ts +104 -0
- package/async.js +234 -0
- package/async.js.map +1 -0
- package/crank.cjs +1672 -1313
- package/crank.cjs.map +1 -1
- package/crank.d.ts +230 -247
- package/crank.js +1672 -1314
- package/crank.js.map +1 -1
- package/dom.cjs +356 -212
- package/dom.cjs.map +1 -1
- package/dom.d.ts +5 -5
- package/dom.js +357 -213
- package/dom.js.map +1 -1
- package/event-target.cjs +254 -0
- package/event-target.cjs.map +1 -0
- package/event-target.d.ts +26 -0
- package/event-target.js +249 -0
- package/event-target.js.map +1 -0
- package/html.cjs +32 -43
- package/html.cjs.map +1 -1
- package/html.d.ts +3 -3
- package/html.js +33 -44
- package/html.js.map +1 -1
- package/jsx-runtime.cjs +1 -0
- package/jsx-runtime.cjs.map +1 -1
- package/jsx-runtime.js +1 -0
- package/jsx-runtime.js.map +1 -1
- package/jsx-tag.cjs +6 -2
- package/jsx-tag.cjs.map +1 -1
- package/jsx-tag.d.ts +2 -0
- package/jsx-tag.js +6 -3
- package/jsx-tag.js.map +1 -1
- package/package.json +46 -30
- package/standalone.cjs +3 -0
- package/standalone.cjs.map +1 -1
- package/standalone.d.ts +1 -1
- package/standalone.js +3 -2
- package/standalone.js.map +1 -1
- package/umd.js +2292 -1562
- package/umd.js.map +1 -1
package/_utils.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare function wrap<T>(value: Array<T> | T | undefined): Array<T>;
|
|
2
|
+
export declare function unwrap<T>(arr: Array<T>): Array<T> | T | undefined;
|
|
3
|
+
export type NonStringIterable<T> = Iterable<T> & object;
|
|
4
|
+
/**
|
|
5
|
+
* Ensures a value is an array.
|
|
6
|
+
*
|
|
7
|
+
* This function does the same thing as wrap() above except it handles nulls
|
|
8
|
+
* and iterables, so it is appropriate for wrapping user-provided element
|
|
9
|
+
* children.
|
|
10
|
+
*/
|
|
11
|
+
export declare function arrayify<T>(value: NonStringIterable<T> | T | null | undefined): Array<T>;
|
|
12
|
+
export declare function isIteratorLike(value: any): value is Iterator<unknown> | AsyncIterator<unknown>;
|
|
13
|
+
export declare function isPromiseLike(value: any): value is PromiseLike<unknown>;
|
|
14
|
+
export declare function safeRace<T>(contenders: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>>;
|
package/async.cjs
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var crank = require('./crank.cjs');
|
|
4
|
+
require('./event-target.cjs');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Creates a lazy-loaded component from an initializer function.
|
|
8
|
+
*
|
|
9
|
+
* @param initializer - Function that returns a Promise resolving to a component or module
|
|
10
|
+
* @returns A component that loads the target component on first render
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```jsx
|
|
14
|
+
* const LazyComponent = lazy(() => import('./MyComponent'));
|
|
15
|
+
*
|
|
16
|
+
* <Suspense fallback={<div>Loading...</div>}>
|
|
17
|
+
* <LazyComponent prop="value" />
|
|
18
|
+
* </Suspense>
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
function lazy(initializer) {
|
|
22
|
+
return async function* LazyComponent(props) {
|
|
23
|
+
let Component = await initializer();
|
|
24
|
+
if (Component && typeof Component === "object" && "default" in Component) {
|
|
25
|
+
Component = Component.default;
|
|
26
|
+
}
|
|
27
|
+
if (typeof Component !== "function") {
|
|
28
|
+
throw new Error("Lazy component initializer must return a Component or a module with a default export that is a Component.");
|
|
29
|
+
}
|
|
30
|
+
for (props of this) {
|
|
31
|
+
yield crank.createElement(Component, props);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
async function SuspenseEmpty() {
|
|
36
|
+
await new Promise((resolve) => setTimeout(resolve));
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
async function SuspenseFallback({ children, timeout, schedule, }) {
|
|
40
|
+
if (schedule) {
|
|
41
|
+
this.schedule(schedule);
|
|
42
|
+
}
|
|
43
|
+
await new Promise((resolve) => setTimeout(resolve, timeout));
|
|
44
|
+
return children;
|
|
45
|
+
}
|
|
46
|
+
function SuspenseChildren({ children, schedule, }) {
|
|
47
|
+
if (schedule) {
|
|
48
|
+
this.schedule(schedule);
|
|
49
|
+
}
|
|
50
|
+
return children;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A component that displays a fallback while its children are loading.
|
|
54
|
+
*
|
|
55
|
+
* When used within a SuspenseList, coordinates with siblings to control
|
|
56
|
+
* reveal order and fallback behavior.
|
|
57
|
+
*
|
|
58
|
+
* @param children - The content to display when loading is complete
|
|
59
|
+
* @param fallback - The content to display while children are loading
|
|
60
|
+
* @param timeout - Time in milliseconds before showing fallback (defaults to
|
|
61
|
+
* 300ms standalone, or inherits from SuspenseList)
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```jsx
|
|
65
|
+
* <Suspense fallback={<div>Loading...</div>}>
|
|
66
|
+
* <AsyncComponent />
|
|
67
|
+
* </Suspense>
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
async function* Suspense({ children, fallback, timeout, }) {
|
|
71
|
+
const controller = this.consume(SuspenseListController);
|
|
72
|
+
if (controller) {
|
|
73
|
+
controller.register(this);
|
|
74
|
+
}
|
|
75
|
+
this.provide(SuspenseListController, undefined);
|
|
76
|
+
let initial = true;
|
|
77
|
+
for await ({ children, fallback, timeout } of this) {
|
|
78
|
+
if (timeout == null) {
|
|
79
|
+
if (controller) {
|
|
80
|
+
timeout = controller.timeout;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
timeout = 300;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (!controller) {
|
|
87
|
+
yield crank.createElement(SuspenseFallback, {
|
|
88
|
+
timeout: timeout,
|
|
89
|
+
children: fallback,
|
|
90
|
+
});
|
|
91
|
+
yield children;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (controller.revealOrder !== "together") {
|
|
95
|
+
if (!controller.isHead(this)) {
|
|
96
|
+
yield crank.createElement(SuspenseEmpty);
|
|
97
|
+
}
|
|
98
|
+
if (controller.tail !== "hidden") {
|
|
99
|
+
yield crank.createElement(SuspenseFallback, {
|
|
100
|
+
timeout: timeout,
|
|
101
|
+
schedule: initial
|
|
102
|
+
? () => controller.scheduleFallback(this)
|
|
103
|
+
: undefined,
|
|
104
|
+
children: fallback,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
yield crank.createElement(SuspenseChildren, {
|
|
109
|
+
schedule: initial ? () => controller.scheduleChildren(this) : undefined,
|
|
110
|
+
children,
|
|
111
|
+
});
|
|
112
|
+
initial = false;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const SuspenseListController = Symbol.for("SuspenseListController");
|
|
116
|
+
/**
|
|
117
|
+
* Coordinates the reveal order of multiple <Suspense> children.
|
|
118
|
+
*
|
|
119
|
+
* Controls when child <Suspense> components show their content or fallbacks
|
|
120
|
+
* based on the specified reveal order. The <SuspenseList> resolves when
|
|
121
|
+
* coordination effort is complete (not necessarily when all content is
|
|
122
|
+
* loaded).
|
|
123
|
+
*
|
|
124
|
+
* @param revealOrder - How children should be revealed:
|
|
125
|
+
* - "forwards" (default): Show children in document order, waiting for
|
|
126
|
+
* predecessors
|
|
127
|
+
* - "backwards": Show children in reverse order, waiting for successors
|
|
128
|
+
* - "together": Show all children simultaneously when all are ready
|
|
129
|
+
* In Crank, the default behavior of async components is to render together,
|
|
130
|
+
* so "together" might not be necessary if you are not using <Suspense>
|
|
131
|
+
* fallbacks.
|
|
132
|
+
* @param tail - How to handle fallbacks:
|
|
133
|
+
* - "collapsed" (default): Show only the fallback for the next unresolved
|
|
134
|
+
* Suspense component
|
|
135
|
+
* - "hidden": Hide all fallbacks
|
|
136
|
+
* Tail behavior only applies when revealOrder is not "together".
|
|
137
|
+
* @param timeout - Default timeout for Suspense children in milliseconds
|
|
138
|
+
* @param children - The elements containing Suspense components to coordinate.
|
|
139
|
+
* Suspense components which are not rendered immediately (because they are
|
|
140
|
+
* the children of another async component) will not be coordinated.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```jsx
|
|
144
|
+
* <SuspenseList revealOrder="forwards" tail="collapsed">
|
|
145
|
+
* <Suspense fallback={<div>Loading A...</div>}>
|
|
146
|
+
* <ComponentA />
|
|
147
|
+
* </Suspense>
|
|
148
|
+
* <Suspense fallback={<div>Loading B...</div>}>
|
|
149
|
+
* <ComponentB />
|
|
150
|
+
* </Suspense>
|
|
151
|
+
* </SuspenseList>
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
function* SuspenseList({ revealOrder = "forwards", tail = "collapsed", timeout, children, }) {
|
|
155
|
+
let registering = true;
|
|
156
|
+
const suspenseItems = [];
|
|
157
|
+
const controller = {
|
|
158
|
+
timeout,
|
|
159
|
+
revealOrder,
|
|
160
|
+
tail,
|
|
161
|
+
register(ctx) {
|
|
162
|
+
if (registering) {
|
|
163
|
+
let childrenResolver;
|
|
164
|
+
const childrenPromise = new Promise((r) => (childrenResolver = r));
|
|
165
|
+
suspenseItems.push({
|
|
166
|
+
ctx,
|
|
167
|
+
childrenResolver: childrenResolver,
|
|
168
|
+
childrenPromise,
|
|
169
|
+
});
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
isHead(ctx) {
|
|
174
|
+
const index = suspenseItems.findIndex((item) => item.ctx === ctx);
|
|
175
|
+
if (index === -1) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
if (revealOrder === "forwards") {
|
|
179
|
+
return index === 0;
|
|
180
|
+
}
|
|
181
|
+
else if (revealOrder === "backwards") {
|
|
182
|
+
return index === suspenseItems.length - 1;
|
|
183
|
+
}
|
|
184
|
+
return false;
|
|
185
|
+
},
|
|
186
|
+
async scheduleFallback(ctx) {
|
|
187
|
+
const index = suspenseItems.findIndex((item) => item.ctx === ctx);
|
|
188
|
+
if (index === -1) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
else if (revealOrder === "forwards") {
|
|
192
|
+
await Promise.all(suspenseItems.slice(0, index).map((item) => item.childrenPromise));
|
|
193
|
+
}
|
|
194
|
+
else if (revealOrder === "backwards") {
|
|
195
|
+
await Promise.all(suspenseItems.slice(index + 1).map((item) => item.childrenPromise));
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
async scheduleChildren(ctx) {
|
|
199
|
+
const index = suspenseItems.findIndex((item) => item.ctx === ctx);
|
|
200
|
+
if (index === -1) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
// This children content is ready
|
|
204
|
+
suspenseItems[index].childrenResolver();
|
|
205
|
+
// Children coordination - determine when this content should show
|
|
206
|
+
if (revealOrder === "together") {
|
|
207
|
+
await Promise.all(suspenseItems.map((item) => item.childrenPromise));
|
|
208
|
+
}
|
|
209
|
+
else if (revealOrder === "forwards") {
|
|
210
|
+
await Promise.all(suspenseItems.slice(0, index + 1).map((item) => item.childrenPromise));
|
|
211
|
+
}
|
|
212
|
+
else if (revealOrder === "backwards") {
|
|
213
|
+
await Promise.all(suspenseItems.slice(index).map((item) => item.childrenPromise));
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
this.provide(SuspenseListController, controller);
|
|
218
|
+
for ({
|
|
219
|
+
revealOrder = "forwards",
|
|
220
|
+
tail = "collapsed",
|
|
221
|
+
timeout,
|
|
222
|
+
children,
|
|
223
|
+
} of this) {
|
|
224
|
+
registering = true;
|
|
225
|
+
// TODO: Is there a fixed amount of microtasks that we can wait for?
|
|
226
|
+
setTimeout(() => (registering = false));
|
|
227
|
+
controller.timeout = timeout;
|
|
228
|
+
controller.revealOrder = revealOrder;
|
|
229
|
+
controller.tail = tail;
|
|
230
|
+
yield children;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
exports.Suspense = Suspense;
|
|
235
|
+
exports.SuspenseList = SuspenseList;
|
|
236
|
+
exports.lazy = lazy;
|
|
237
|
+
//# sourceMappingURL=async.cjs.map
|
package/async.cjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async.cjs","sources":["../src/async.ts"],"sourcesContent":["import type {Children, Component, Context} from \"./crank.js\";\nimport {createElement} from \"./crank.js\";\n\n/**\n * Creates a lazy-loaded component from an initializer function.\n *\n * @param initializer - Function that returns a Promise resolving to a component or module\n * @returns A component that loads the target component on first render\n *\n * @example\n * ```jsx\n * const LazyComponent = lazy(() => import('./MyComponent'));\n *\n * <Suspense fallback={<div>Loading...</div>}>\n * <LazyComponent prop=\"value\" />\n * </Suspense>\n * ```\n */\nexport function lazy<T extends Component>(\n\tinitializer: () => Promise<T | {default: T}>,\n): T {\n\treturn async function* LazyComponent(\n\t\tthis: Context,\n\t\tprops: any,\n\t): AsyncGenerator<Children> {\n\t\tlet Component = await initializer();\n\t\tif (Component && typeof Component === \"object\" && \"default\" in Component) {\n\t\t\tComponent = Component.default;\n\t\t}\n\n\t\tif (typeof Component !== \"function\") {\n\t\t\tthrow new Error(\n\t\t\t\t\"Lazy component initializer must return a Component or a module with a default export that is a Component.\",\n\t\t\t);\n\t\t}\n\n\t\tfor (props of this) {\n\t\t\tyield createElement(Component, props);\n\t\t}\n\t} as unknown as T;\n}\n\nasync function SuspenseEmpty() {\n\tawait new Promise((resolve) => setTimeout(resolve));\n\treturn null;\n}\n\nasync function SuspenseFallback(\n\tthis: Context,\n\t{\n\t\tchildren,\n\t\ttimeout,\n\t\tschedule,\n\t}: {\n\t\tchildren: Children;\n\t\ttimeout: number;\n\t\tschedule?: () => Promise<unknown>;\n\t},\n): Promise<Children> {\n\tif (schedule) {\n\t\tthis.schedule(schedule);\n\t}\n\n\tawait new Promise((resolve) => setTimeout(resolve, timeout));\n\treturn children;\n}\n\nfunction SuspenseChildren(\n\tthis: Context,\n\t{\n\t\tchildren,\n\t\tschedule,\n\t}: {\n\t\tchildren: Children;\n\t\tschedule?: () => Promise<unknown>;\n\t},\n) {\n\tif (schedule) {\n\t\tthis.schedule(schedule);\n\t}\n\n\treturn children;\n}\n\n/**\n * A component that displays a fallback while its children are loading.\n *\n * When used within a SuspenseList, coordinates with siblings to control\n * reveal order and fallback behavior.\n *\n * @param children - The content to display when loading is complete\n * @param fallback - The content to display while children are loading\n * @param timeout - Time in milliseconds before showing fallback (defaults to\n * 300ms standalone, or inherits from SuspenseList)\n *\n * @example\n * ```jsx\n * <Suspense fallback={<div>Loading...</div>}>\n * <AsyncComponent />\n * </Suspense>\n * ```\n */\nexport async function* Suspense(\n\tthis: Context,\n\t{\n\t\tchildren,\n\t\tfallback,\n\t\ttimeout,\n\t}: {children: Children; fallback: Children; timeout?: number},\n): AsyncGenerator<Children> {\n\tconst controller = this.consume(SuspenseListController);\n\tif (controller) {\n\t\tcontroller.register(this);\n\t}\n\n\tthis.provide(SuspenseListController, undefined);\n\tlet initial = true;\n\tfor await ({children, fallback, timeout} of this) {\n\t\tif (timeout == null) {\n\t\t\tif (controller) {\n\t\t\t\ttimeout = controller.timeout;\n\t\t\t} else {\n\t\t\t\ttimeout = 300;\n\t\t\t}\n\t\t}\n\n\t\tif (!controller) {\n\t\t\tyield createElement(SuspenseFallback, {\n\t\t\t\ttimeout: timeout!,\n\t\t\t\tchildren: fallback,\n\t\t\t});\n\t\t\tyield children;\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (controller.revealOrder !== \"together\") {\n\t\t\tif (!controller.isHead(this)) {\n\t\t\t\tyield createElement(SuspenseEmpty);\n\t\t\t}\n\n\t\t\tif (controller.tail !== \"hidden\") {\n\t\t\t\tyield createElement(SuspenseFallback, {\n\t\t\t\t\ttimeout: timeout!,\n\t\t\t\t\tschedule: initial\n\t\t\t\t\t\t? () => controller.scheduleFallback(this)\n\t\t\t\t\t\t: undefined,\n\t\t\t\t\tchildren: fallback,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tyield createElement(SuspenseChildren, {\n\t\t\tschedule: initial ? () => controller.scheduleChildren(this) : undefined,\n\t\t\tchildren,\n\t\t});\n\n\t\tinitial = false;\n\t}\n}\n\nconst SuspenseListController = Symbol.for(\"SuspenseListController\");\n\ninterface SuspenseListController {\n\ttimeout?: number;\n\trevealOrder?: \"forwards\" | \"backwards\" | \"together\";\n\ttail?: \"collapsed\" | \"hidden\";\n\tregister(ctx: Context): void;\n\tisHead(ctx: Context): boolean;\n\tscheduleFallback(ctx: Context): Promise<void>;\n\tscheduleChildren(ctx: Context): Promise<void>;\n}\n\ndeclare global {\n\tnamespace Crank {\n\t\tinterface ProvisionMap {\n\t\t\t[SuspenseListController]: SuspenseListController;\n\t\t}\n\t}\n}\n\n/**\n * Coordinates the reveal order of multiple <Suspense> children.\n *\n * Controls when child <Suspense> components show their content or fallbacks\n * based on the specified reveal order. The <SuspenseList> resolves when\n * coordination effort is complete (not necessarily when all content is\n * loaded).\n *\n * @param revealOrder - How children should be revealed:\n * - \"forwards\" (default): Show children in document order, waiting for\n * predecessors\n * - \"backwards\": Show children in reverse order, waiting for successors\n * - \"together\": Show all children simultaneously when all are ready\n * In Crank, the default behavior of async components is to render together,\n * so \"together\" might not be necessary if you are not using <Suspense>\n * fallbacks.\n * @param tail - How to handle fallbacks:\n * - \"collapsed\" (default): Show only the fallback for the next unresolved\n * Suspense component\n * - \"hidden\": Hide all fallbacks\n * Tail behavior only applies when revealOrder is not \"together\".\n * @param timeout - Default timeout for Suspense children in milliseconds\n * @param children - The elements containing Suspense components to coordinate.\n * Suspense components which are not rendered immediately (because they are\n * the children of another async component) will not be coordinated.\n *\n * @example\n * ```jsx\n * <SuspenseList revealOrder=\"forwards\" tail=\"collapsed\">\n * <Suspense fallback={<div>Loading A...</div>}>\n * <ComponentA />\n * </Suspense>\n * <Suspense fallback={<div>Loading B...</div>}>\n * <ComponentB />\n * </Suspense>\n * </SuspenseList>\n * ```\n */\nexport function* SuspenseList(\n\tthis: Context,\n\t{\n\t\trevealOrder = \"forwards\",\n\t\ttail = \"collapsed\",\n\t\ttimeout,\n\t\tchildren,\n\t}: {\n\t\trevealOrder?: \"forwards\" | \"backwards\" | \"together\";\n\t\ttail?: \"collapsed\" | \"hidden\";\n\t\ttimeout?: number;\n\t\tchildren: Children;\n\t},\n): Generator<Children> {\n\tlet registering = true;\n\tconst suspenseItems: Array<{\n\t\tctx: Context;\n\t\tchildrenResolver: () => void;\n\t\tchildrenPromise: Promise<void>;\n\t}> = [];\n\n\tconst controller: SuspenseListController = {\n\t\ttimeout,\n\t\trevealOrder,\n\t\ttail,\n\t\tregister(ctx: Context) {\n\t\t\tif (registering) {\n\t\t\t\tlet childrenResolver: () => void;\n\n\t\t\t\tconst childrenPromise = new Promise<void>(\n\t\t\t\t\t(r) => (childrenResolver = r),\n\t\t\t\t);\n\n\t\t\t\tsuspenseItems.push({\n\t\t\t\t\tctx,\n\t\t\t\t\tchildrenResolver: childrenResolver!,\n\t\t\t\t\tchildrenPromise,\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\t\t},\n\n\t\tisHead(ctx: Context): boolean {\n\t\t\tconst index = suspenseItems.findIndex((item) => item.ctx === ctx);\n\t\t\tif (index === -1) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tif (revealOrder === \"forwards\") {\n\t\t\t\treturn index === 0;\n\t\t\t} else if (revealOrder === \"backwards\") {\n\t\t\t\treturn index === suspenseItems.length - 1;\n\t\t\t}\n\n\t\t\treturn false;\n\t\t},\n\n\t\tasync scheduleFallback(ctx: Context) {\n\t\t\tconst index = suspenseItems.findIndex((item) => item.ctx === ctx);\n\t\t\tif (index === -1) {\n\t\t\t\treturn;\n\t\t\t} else if (revealOrder === \"forwards\") {\n\t\t\t\tawait Promise.all(\n\t\t\t\t\tsuspenseItems.slice(0, index).map((item) => item.childrenPromise),\n\t\t\t\t);\n\t\t\t} else if (revealOrder === \"backwards\") {\n\t\t\t\tawait Promise.all(\n\t\t\t\t\tsuspenseItems.slice(index + 1).map((item) => item.childrenPromise),\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\n\t\tasync scheduleChildren(ctx: Context) {\n\t\t\tconst index = suspenseItems.findIndex((item) => item.ctx === ctx);\n\t\t\tif (index === -1) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// This children content is ready\n\t\t\tsuspenseItems[index].childrenResolver();\n\t\t\t// Children coordination - determine when this content should show\n\t\t\tif (revealOrder === \"together\") {\n\t\t\t\tawait Promise.all(suspenseItems.map((item) => item.childrenPromise));\n\t\t\t} else if (revealOrder === \"forwards\") {\n\t\t\t\tawait Promise.all(\n\t\t\t\t\tsuspenseItems.slice(0, index + 1).map((item) => item.childrenPromise),\n\t\t\t\t);\n\t\t\t} else if (revealOrder === \"backwards\") {\n\t\t\t\tawait Promise.all(\n\t\t\t\t\tsuspenseItems.slice(index).map((item) => item.childrenPromise),\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\t};\n\n\tthis.provide(SuspenseListController, controller);\n\tfor ({\n\t\trevealOrder = \"forwards\",\n\t\ttail = \"collapsed\",\n\t\ttimeout,\n\t\tchildren,\n\t} of this) {\n\t\tregistering = true;\n\t\t// TODO: Is there a fixed amount of microtasks that we can wait for?\n\t\tsetTimeout(() => (registering = false));\n\t\tcontroller.timeout = timeout;\n\t\tcontroller.revealOrder = revealOrder;\n\t\tcontroller.tail = tail;\n\t\tyield children;\n\t}\n}\n"],"names":["createElement"],"mappings":";;;;;AAGA;;;;;;;;;;;;;;AAcG;AACG,SAAU,IAAI,CACnB,WAA4C,EAAA;AAE5C,IAAA,OAAO,gBAAgB,aAAa,CAEnC,KAAU,EAAA;AAEV,QAAA,IAAI,SAAS,GAAG,MAAM,WAAW,EAAE;QACnC,IAAI,SAAS,IAAI,O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ClB,OAAO;QACP,QAAQ;KACR,IAAI,IAAI,EAAE;QACV,WAAW,GAAG,IAAI;;QAElB,UAAU,CAAC,OAAO,WAAW,GAAG,KAAK,CAAC,CAAC;AACvC,QAAA,UAAU,CAAC,OAAO,GAAG,OAAO;AAC5B,QAAA,UAAU,CAAC,WAAW,GAAG,WAAW;AACpC,QAAA,UAAU,CAAC,IAAI,GAAG,IAAI;AACtB,QAAA,MAAM,QAAQ;;AAEhB;;;;;;"}
|
package/async.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { Children, Component, Context } from "./crank.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a lazy-loaded component from an initializer function.
|
|
4
|
+
*
|
|
5
|
+
* @param initializer - Function that returns a Promise resolving to a component or module
|
|
6
|
+
* @returns A component that loads the target component on first render
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```jsx
|
|
10
|
+
* const LazyComponent = lazy(() => import('./MyComponent'));
|
|
11
|
+
*
|
|
12
|
+
* <Suspense fallback={<div>Loading...</div>}>
|
|
13
|
+
* <LazyComponent prop="value" />
|
|
14
|
+
* </Suspense>
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function lazy<T extends Component>(initializer: () => Promise<T | {
|
|
18
|
+
default: T;
|
|
19
|
+
}>): T;
|
|
20
|
+
/**
|
|
21
|
+
* A component that displays a fallback while its children are loading.
|
|
22
|
+
*
|
|
23
|
+
* When used within a SuspenseList, coordinates with siblings to control
|
|
24
|
+
* reveal order and fallback behavior.
|
|
25
|
+
*
|
|
26
|
+
* @param children - The content to display when loading is complete
|
|
27
|
+
* @param fallback - The content to display while children are loading
|
|
28
|
+
* @param timeout - Time in milliseconds before showing fallback (defaults to
|
|
29
|
+
* 300ms standalone, or inherits from SuspenseList)
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```jsx
|
|
33
|
+
* <Suspense fallback={<div>Loading...</div>}>
|
|
34
|
+
* <AsyncComponent />
|
|
35
|
+
* </Suspense>
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function Suspense(this: Context, { children, fallback, timeout, }: {
|
|
39
|
+
children: Children;
|
|
40
|
+
fallback: Children;
|
|
41
|
+
timeout?: number;
|
|
42
|
+
}): AsyncGenerator<Children>;
|
|
43
|
+
declare const SuspenseListController: unique symbol;
|
|
44
|
+
interface SuspenseListController {
|
|
45
|
+
timeout?: number;
|
|
46
|
+
revealOrder?: "forwards" | "backwards" | "together";
|
|
47
|
+
tail?: "collapsed" | "hidden";
|
|
48
|
+
register(ctx: Context): void;
|
|
49
|
+
isHead(ctx: Context): boolean;
|
|
50
|
+
scheduleFallback(ctx: Context): Promise<void>;
|
|
51
|
+
scheduleChildren(ctx: Context): Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
declare global {
|
|
54
|
+
namespace Crank {
|
|
55
|
+
interface ProvisionMap {
|
|
56
|
+
[SuspenseListController]: SuspenseListController;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Coordinates the reveal order of multiple <Suspense> children.
|
|
62
|
+
*
|
|
63
|
+
* Controls when child <Suspense> components show their content or fallbacks
|
|
64
|
+
* based on the specified reveal order. The <SuspenseList> resolves when
|
|
65
|
+
* coordination effort is complete (not necessarily when all content is
|
|
66
|
+
* loaded).
|
|
67
|
+
*
|
|
68
|
+
* @param revealOrder - How children should be revealed:
|
|
69
|
+
* - "forwards" (default): Show children in document order, waiting for
|
|
70
|
+
* predecessors
|
|
71
|
+
* - "backwards": Show children in reverse order, waiting for successors
|
|
72
|
+
* - "together": Show all children simultaneously when all are ready
|
|
73
|
+
* In Crank, the default behavior of async components is to render together,
|
|
74
|
+
* so "together" might not be necessary if you are not using <Suspense>
|
|
75
|
+
* fallbacks.
|
|
76
|
+
* @param tail - How to handle fallbacks:
|
|
77
|
+
* - "collapsed" (default): Show only the fallback for the next unresolved
|
|
78
|
+
* Suspense component
|
|
79
|
+
* - "hidden": Hide all fallbacks
|
|
80
|
+
* Tail behavior only applies when revealOrder is not "together".
|
|
81
|
+
* @param timeout - Default timeout for Suspense children in milliseconds
|
|
82
|
+
* @param children - The elements containing Suspense components to coordinate.
|
|
83
|
+
* Suspense components which are not rendered immediately (because they are
|
|
84
|
+
* the children of another async component) will not be coordinated.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```jsx
|
|
88
|
+
* <SuspenseList revealOrder="forwards" tail="collapsed">
|
|
89
|
+
* <Suspense fallback={<div>Loading A...</div>}>
|
|
90
|
+
* <ComponentA />
|
|
91
|
+
* </Suspense>
|
|
92
|
+
* <Suspense fallback={<div>Loading B...</div>}>
|
|
93
|
+
* <ComponentB />
|
|
94
|
+
* </Suspense>
|
|
95
|
+
* </SuspenseList>
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export declare function SuspenseList(this: Context, { revealOrder, tail, timeout, children, }: {
|
|
99
|
+
revealOrder?: "forwards" | "backwards" | "together";
|
|
100
|
+
tail?: "collapsed" | "hidden";
|
|
101
|
+
timeout?: number;
|
|
102
|
+
children: Children;
|
|
103
|
+
}): Generator<Children>;
|
|
104
|
+
export {};
|
package/async.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/// <reference types="async.d.ts" />
|
|
2
|
+
import { createElement } from './crank.js';
|
|
3
|
+
import './event-target.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a lazy-loaded component from an initializer function.
|
|
7
|
+
*
|
|
8
|
+
* @param initializer - Function that returns a Promise resolving to a component or module
|
|
9
|
+
* @returns A component that loads the target component on first render
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```jsx
|
|
13
|
+
* const LazyComponent = lazy(() => import('./MyComponent'));
|
|
14
|
+
*
|
|
15
|
+
* <Suspense fallback={<div>Loading...</div>}>
|
|
16
|
+
* <LazyComponent prop="value" />
|
|
17
|
+
* </Suspense>
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
function lazy(initializer) {
|
|
21
|
+
return async function* LazyComponent(props) {
|
|
22
|
+
let Component = await initializer();
|
|
23
|
+
if (Component && typeof Component === "object" && "default" in Component) {
|
|
24
|
+
Component = Component.default;
|
|
25
|
+
}
|
|
26
|
+
if (typeof Component !== "function") {
|
|
27
|
+
throw new Error("Lazy component initializer must return a Component or a module with a default export that is a Component.");
|
|
28
|
+
}
|
|
29
|
+
for (props of this) {
|
|
30
|
+
yield createElement(Component, props);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
async function SuspenseEmpty() {
|
|
35
|
+
await new Promise((resolve) => setTimeout(resolve));
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
async function SuspenseFallback({ children, timeout, schedule, }) {
|
|
39
|
+
if (schedule) {
|
|
40
|
+
this.schedule(schedule);
|
|
41
|
+
}
|
|
42
|
+
await new Promise((resolve) => setTimeout(resolve, timeout));
|
|
43
|
+
return children;
|
|
44
|
+
}
|
|
45
|
+
function SuspenseChildren({ children, schedule, }) {
|
|
46
|
+
if (schedule) {
|
|
47
|
+
this.schedule(schedule);
|
|
48
|
+
}
|
|
49
|
+
return children;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A component that displays a fallback while its children are loading.
|
|
53
|
+
*
|
|
54
|
+
* When used within a SuspenseList, coordinates with siblings to control
|
|
55
|
+
* reveal order and fallback behavior.
|
|
56
|
+
*
|
|
57
|
+
* @param children - The content to display when loading is complete
|
|
58
|
+
* @param fallback - The content to display while children are loading
|
|
59
|
+
* @param timeout - Time in milliseconds before showing fallback (defaults to
|
|
60
|
+
* 300ms standalone, or inherits from SuspenseList)
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```jsx
|
|
64
|
+
* <Suspense fallback={<div>Loading...</div>}>
|
|
65
|
+
* <AsyncComponent />
|
|
66
|
+
* </Suspense>
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
async function* Suspense({ children, fallback, timeout, }) {
|
|
70
|
+
const controller = this.consume(SuspenseListController);
|
|
71
|
+
if (controller) {
|
|
72
|
+
controller.register(this);
|
|
73
|
+
}
|
|
74
|
+
this.provide(SuspenseListController, undefined);
|
|
75
|
+
let initial = true;
|
|
76
|
+
for await ({ children, fallback, timeout } of this) {
|
|
77
|
+
if (timeout == null) {
|
|
78
|
+
if (controller) {
|
|
79
|
+
timeout = controller.timeout;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
timeout = 300;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (!controller) {
|
|
86
|
+
yield createElement(SuspenseFallback, {
|
|
87
|
+
timeout: timeout,
|
|
88
|
+
children: fallback,
|
|
89
|
+
});
|
|
90
|
+
yield children;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (controller.revealOrder !== "together") {
|
|
94
|
+
if (!controller.isHead(this)) {
|
|
95
|
+
yield createElement(SuspenseEmpty);
|
|
96
|
+
}
|
|
97
|
+
if (controller.tail !== "hidden") {
|
|
98
|
+
yield createElement(SuspenseFallback, {
|
|
99
|
+
timeout: timeout,
|
|
100
|
+
schedule: initial
|
|
101
|
+
? () => controller.scheduleFallback(this)
|
|
102
|
+
: undefined,
|
|
103
|
+
children: fallback,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
yield createElement(SuspenseChildren, {
|
|
108
|
+
schedule: initial ? () => controller.scheduleChildren(this) : undefined,
|
|
109
|
+
children,
|
|
110
|
+
});
|
|
111
|
+
initial = false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const SuspenseListController = Symbol.for("SuspenseListController");
|
|
115
|
+
/**
|
|
116
|
+
* Coordinates the reveal order of multiple <Suspense> children.
|
|
117
|
+
*
|
|
118
|
+
* Controls when child <Suspense> components show their content or fallbacks
|
|
119
|
+
* based on the specified reveal order. The <SuspenseList> resolves when
|
|
120
|
+
* coordination effort is complete (not necessarily when all content is
|
|
121
|
+
* loaded).
|
|
122
|
+
*
|
|
123
|
+
* @param revealOrder - How children should be revealed:
|
|
124
|
+
* - "forwards" (default): Show children in document order, waiting for
|
|
125
|
+
* predecessors
|
|
126
|
+
* - "backwards": Show children in reverse order, waiting for successors
|
|
127
|
+
* - "together": Show all children simultaneously when all are ready
|
|
128
|
+
* In Crank, the default behavior of async components is to render together,
|
|
129
|
+
* so "together" might not be necessary if you are not using <Suspense>
|
|
130
|
+
* fallbacks.
|
|
131
|
+
* @param tail - How to handle fallbacks:
|
|
132
|
+
* - "collapsed" (default): Show only the fallback for the next unresolved
|
|
133
|
+
* Suspense component
|
|
134
|
+
* - "hidden": Hide all fallbacks
|
|
135
|
+
* Tail behavior only applies when revealOrder is not "together".
|
|
136
|
+
* @param timeout - Default timeout for Suspense children in milliseconds
|
|
137
|
+
* @param children - The elements containing Suspense components to coordinate.
|
|
138
|
+
* Suspense components which are not rendered immediately (because they are
|
|
139
|
+
* the children of another async component) will not be coordinated.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```jsx
|
|
143
|
+
* <SuspenseList revealOrder="forwards" tail="collapsed">
|
|
144
|
+
* <Suspense fallback={<div>Loading A...</div>}>
|
|
145
|
+
* <ComponentA />
|
|
146
|
+
* </Suspense>
|
|
147
|
+
* <Suspense fallback={<div>Loading B...</div>}>
|
|
148
|
+
* <ComponentB />
|
|
149
|
+
* </Suspense>
|
|
150
|
+
* </SuspenseList>
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
function* SuspenseList({ revealOrder = "forwards", tail = "collapsed", timeout, children, }) {
|
|
154
|
+
let registering = true;
|
|
155
|
+
const suspenseItems = [];
|
|
156
|
+
const controller = {
|
|
157
|
+
timeout,
|
|
158
|
+
revealOrder,
|
|
159
|
+
tail,
|
|
160
|
+
register(ctx) {
|
|
161
|
+
if (registering) {
|
|
162
|
+
let childrenResolver;
|
|
163
|
+
const childrenPromise = new Promise((r) => (childrenResolver = r));
|
|
164
|
+
suspenseItems.push({
|
|
165
|
+
ctx,
|
|
166
|
+
childrenResolver: childrenResolver,
|
|
167
|
+
childrenPromise,
|
|
168
|
+
});
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
isHead(ctx) {
|
|
173
|
+
const index = suspenseItems.findIndex((item) => item.ctx === ctx);
|
|
174
|
+
if (index === -1) {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
if (revealOrder === "forwards") {
|
|
178
|
+
return index === 0;
|
|
179
|
+
}
|
|
180
|
+
else if (revealOrder === "backwards") {
|
|
181
|
+
return index === suspenseItems.length - 1;
|
|
182
|
+
}
|
|
183
|
+
return false;
|
|
184
|
+
},
|
|
185
|
+
async scheduleFallback(ctx) {
|
|
186
|
+
const index = suspenseItems.findIndex((item) => item.ctx === ctx);
|
|
187
|
+
if (index === -1) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
else if (revealOrder === "forwards") {
|
|
191
|
+
await Promise.all(suspenseItems.slice(0, index).map((item) => item.childrenPromise));
|
|
192
|
+
}
|
|
193
|
+
else if (revealOrder === "backwards") {
|
|
194
|
+
await Promise.all(suspenseItems.slice(index + 1).map((item) => item.childrenPromise));
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
async scheduleChildren(ctx) {
|
|
198
|
+
const index = suspenseItems.findIndex((item) => item.ctx === ctx);
|
|
199
|
+
if (index === -1) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
// This children content is ready
|
|
203
|
+
suspenseItems[index].childrenResolver();
|
|
204
|
+
// Children coordination - determine when this content should show
|
|
205
|
+
if (revealOrder === "together") {
|
|
206
|
+
await Promise.all(suspenseItems.map((item) => item.childrenPromise));
|
|
207
|
+
}
|
|
208
|
+
else if (revealOrder === "forwards") {
|
|
209
|
+
await Promise.all(suspenseItems.slice(0, index + 1).map((item) => item.childrenPromise));
|
|
210
|
+
}
|
|
211
|
+
else if (revealOrder === "backwards") {
|
|
212
|
+
await Promise.all(suspenseItems.slice(index).map((item) => item.childrenPromise));
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
this.provide(SuspenseListController, controller);
|
|
217
|
+
for ({
|
|
218
|
+
revealOrder = "forwards",
|
|
219
|
+
tail = "collapsed",
|
|
220
|
+
timeout,
|
|
221
|
+
children,
|
|
222
|
+
} of this) {
|
|
223
|
+
registering = true;
|
|
224
|
+
// TODO: Is there a fixed amount of microtasks that we can wait for?
|
|
225
|
+
setTimeout(() => (registering = false));
|
|
226
|
+
controller.timeout = timeout;
|
|
227
|
+
controller.revealOrder = revealOrder;
|
|
228
|
+
controller.tail = tail;
|
|
229
|
+
yield children;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export { Suspense, SuspenseList, lazy };
|
|
234
|
+
//# sourceMappingURL=async.js.map
|