ovr 4.5.1 → 4.6.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/dist/app/context.d.ts.map +1 -1
- package/dist/app/context.js +1 -1
- package/dist/jsx/chunk/index.d.ts +9 -0
- package/dist/jsx/chunk/index.d.ts.map +1 -1
- package/dist/jsx/chunk/index.js +11 -0
- package/dist/jsx/index.d.ts +5 -4
- package/dist/jsx/index.d.ts.map +1 -1
- package/dist/jsx/index.js +36 -41
- package/dist/jsx/merge-async-generators.d.ts.map +1 -1
- package/dist/jsx/merge-async-generators.js +9 -3
- package/package.json +1 -1
- package/src/app/context.ts +1 -3
- package/src/jsx/chunk/index.ts +12 -0
- package/src/jsx/index.ts +39 -53
- package/src/jsx/merge-async-generators.ts +8 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/app/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,GAAG,EAAY,MAAM,iBAAiB,CAAC;AACrD,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEtD,OAAO,EAEN,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,MAAM,YAAY,CAAC;AAGpB,KAAK,MAAM,GAAG,CAAC,KAAK,EAAE;IAAE,QAAQ,EAAE,GAAG,CAAC,OAAO,CAAA;CAAE,KAAK,GAAG,CAAC,OAAO,CAAC;AAoBhE,qBAAa,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;;IAC7C,gFAAgF;IAChF,GAAG,EAAE,OAAO,CAAC;IAEb,4EAA4E;IAC5E,GAAG,EAAE,GAAG,CAAC;IAET;;;;;;;OAOG;IACH,MAAM,EAAG,CAAC,CAAC;IAEX,oCAAoC;IACpC,KAAK,EAAG,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,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KAAK;IA2BL;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,MAAM,CAAC,GAAG;CAUV"}
|
package/dist/app/context.js
CHANGED
|
@@ -200,7 +200,7 @@ export class Context {
|
|
|
200
200
|
}
|
|
201
201
|
elements[3] = bodyParts[0];
|
|
202
202
|
elements.push(Page, Context.#bodyClose + bodyParts[1]);
|
|
203
|
-
stream = toStream(elements.map((el) => typeof el === "string" ?
|
|
203
|
+
stream = toStream(elements.map((el) => (typeof el === "string" ? Chunk.safe(el) : el)));
|
|
204
204
|
}
|
|
205
205
|
else {
|
|
206
206
|
// HTML partial - just use the layouts + page
|
|
@@ -17,6 +17,15 @@ export declare class Chunk {
|
|
|
17
17
|
* @param chunks Chunks to append to the end of the chunk
|
|
18
18
|
*/
|
|
19
19
|
concat(...chunks: Chunk[]): void;
|
|
20
|
+
/**
|
|
21
|
+
* Wraps `new Chunk("html", true)`.
|
|
22
|
+
*
|
|
23
|
+
* Use to create a new `Chunk` and bypass escaping.
|
|
24
|
+
*
|
|
25
|
+
* @param html Safe string of HTML
|
|
26
|
+
* @returns New _safe_ `Chunk`
|
|
27
|
+
*/
|
|
28
|
+
static safe(html: unknown): Chunk;
|
|
20
29
|
/**
|
|
21
30
|
* Escapes strings of HTML.
|
|
22
31
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/jsx/chunk/index.ts"],"names":[],"mappings":"AAAA,qBAAa,KAAK;;IAIjB,2BAA2B;IAC3B,KAAK,EAAE,MAAM,CAAC;IAEd;;;;;OAKG;gBACS,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,OAAO;IAKzC;;OAEG;IACH,QAAQ;IAIR;;OAEG;IACH,MAAM,CAAC,GAAG,MAAM,EAAE,KAAK,EAAE;IAIzB;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;CA2B1C"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/jsx/chunk/index.ts"],"names":[],"mappings":"AAAA,qBAAa,KAAK;;IAIjB,2BAA2B;IAC3B,KAAK,EAAE,MAAM,CAAC;IAEd;;;;;OAKG;gBACS,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,OAAO;IAKzC;;OAEG;IACH,QAAQ;IAIR;;OAEG;IACH,MAAM,CAAC,GAAG,MAAM,EAAE,KAAK,EAAE;IAIzB;;;;;;;OAOG;IACH,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO;IAIzB;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;CA2B1C"}
|
package/dist/jsx/chunk/index.js
CHANGED
|
@@ -25,6 +25,17 @@ export class Chunk {
|
|
|
25
25
|
concat(...chunks) {
|
|
26
26
|
this.value += chunks.join("");
|
|
27
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Wraps `new Chunk("html", true)`.
|
|
30
|
+
*
|
|
31
|
+
* Use to create a new `Chunk` and bypass escaping.
|
|
32
|
+
*
|
|
33
|
+
* @param html Safe string of HTML
|
|
34
|
+
* @returns New _safe_ `Chunk`
|
|
35
|
+
*/
|
|
36
|
+
static safe(html) {
|
|
37
|
+
return new Chunk(html, true);
|
|
38
|
+
}
|
|
28
39
|
/**
|
|
29
40
|
* Escapes strings of HTML.
|
|
30
41
|
*
|
package/dist/jsx/index.d.ts
CHANGED
|
@@ -9,15 +9,16 @@ export declare namespace JSX {
|
|
|
9
9
|
/** ovr Element */
|
|
10
10
|
type Element = MaybeFunction<MaybePromise<string | number | bigint | boolean | object | null | undefined | Symbol | Iterable<Element> | AsyncIterable<Element>>>;
|
|
11
11
|
}
|
|
12
|
-
/** Unknown component props
|
|
12
|
+
/** Unknown component props */
|
|
13
13
|
export type Props = Record<string, JSX.Element>;
|
|
14
14
|
/**
|
|
15
15
|
* The main function of the JSX transform cycle, each time JSX is encountered
|
|
16
|
-
* it is passed into this function to be resolved.
|
|
16
|
+
* it is passed into this function to be resolved. This function doesn't need to be
|
|
17
|
+
* called recursively, JSX will be transformed into these `jsx()` function calls.
|
|
17
18
|
*
|
|
18
19
|
* @param tag string or function component
|
|
19
20
|
* @param props object containing all the properties and attributes passed to the element or component
|
|
20
|
-
* @returns
|
|
21
|
+
* @returns `AsyncGenerator` that yields `Chunk`s of HTML
|
|
21
22
|
*/
|
|
22
23
|
export declare function jsx<P extends Props = Props>(tag: ((props: P) => JSX.Element) | string, props: P): AsyncGenerator<Chunk, void, unknown>;
|
|
23
24
|
/**
|
|
@@ -31,7 +32,7 @@ export declare function Fragment(props?: {
|
|
|
31
32
|
}): AsyncGenerator<Chunk, void, unknown>;
|
|
32
33
|
/**
|
|
33
34
|
* @param element
|
|
34
|
-
* @yields `Chunk`s of HTML as the `Element` resolves
|
|
35
|
+
* @yields `Chunk`s of HTML as the `Element` resolves
|
|
35
36
|
*/
|
|
36
37
|
export declare function toGenerator(element: JSX.Element): AsyncGenerator<Chunk, void, unknown>;
|
|
37
38
|
/**
|
package/dist/jsx/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jsx/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,KAAK,EAAE,iBAAiB,IAAI,EAAE,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jsx/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,O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js";
|
|
3
|
+
import { performance } from "node:perf_hooks";
|
|
3
4
|
import { setImmediate } from "node:timers/promises";
|
|
5
|
+
import { types } from "node:util";
|
|
4
6
|
/**
|
|
5
7
|
* These are the HTML tags that do not require a closing tag.
|
|
6
8
|
*
|
|
@@ -23,11 +25,12 @@ const voidElements = new Set([
|
|
|
23
25
|
]);
|
|
24
26
|
/**
|
|
25
27
|
* The main function of the JSX transform cycle, each time JSX is encountered
|
|
26
|
-
* it is passed into this function to be resolved.
|
|
28
|
+
* it is passed into this function to be resolved. This function doesn't need to be
|
|
29
|
+
* called recursively, JSX will be transformed into these `jsx()` function calls.
|
|
27
30
|
*
|
|
28
31
|
* @param tag string or function component
|
|
29
32
|
* @param props object containing all the properties and attributes passed to the element or component
|
|
30
|
-
* @returns
|
|
33
|
+
* @returns `AsyncGenerator` that yields `Chunk`s of HTML
|
|
31
34
|
*/
|
|
32
35
|
export async function* jsx(tag, props) {
|
|
33
36
|
if (typeof tag === "function") {
|
|
@@ -36,10 +39,13 @@ export async function* jsx(tag, props) {
|
|
|
36
39
|
return;
|
|
37
40
|
}
|
|
38
41
|
// intrinsic element
|
|
39
|
-
|
|
42
|
+
// faster to concatenate attributes than to yield them as separate chunks
|
|
40
43
|
let attributes = "";
|
|
41
|
-
for (const key in
|
|
42
|
-
|
|
44
|
+
for (const key in props) {
|
|
45
|
+
// more memory efficient to skip children instead of destructuring and using ...rest
|
|
46
|
+
if (key === "children")
|
|
47
|
+
continue;
|
|
48
|
+
const value = props[key];
|
|
43
49
|
if (value === true) {
|
|
44
50
|
// just put the key without the value
|
|
45
51
|
attributes += ` ${key}`;
|
|
@@ -52,11 +58,11 @@ export async function* jsx(tag, props) {
|
|
|
52
58
|
}
|
|
53
59
|
// otherwise, don't include the attribute
|
|
54
60
|
}
|
|
55
|
-
yield
|
|
61
|
+
yield Chunk.safe(`<${tag}${attributes}>`);
|
|
56
62
|
if (voidElements.has(tag))
|
|
57
63
|
return;
|
|
58
|
-
yield* toGenerator(children);
|
|
59
|
-
yield
|
|
64
|
+
yield* toGenerator(props.children);
|
|
65
|
+
yield Chunk.safe(`</${tag}>`);
|
|
60
66
|
}
|
|
61
67
|
/**
|
|
62
68
|
* JSX requires a `Fragment` export to resolve `<></>`
|
|
@@ -69,7 +75,7 @@ export async function* Fragment(props = {}) {
|
|
|
69
75
|
}
|
|
70
76
|
/**
|
|
71
77
|
* @param element
|
|
72
|
-
* @yields `Chunk`s of HTML as the `Element` resolves
|
|
78
|
+
* @yields `Chunk`s of HTML as the `Element` resolves
|
|
73
79
|
*/
|
|
74
80
|
export async function* toGenerator(element) {
|
|
75
81
|
// modifications
|
|
@@ -90,59 +96,47 @@ export async function* toGenerator(element) {
|
|
|
90
96
|
}
|
|
91
97
|
if (typeof element === "object") {
|
|
92
98
|
if (Symbol.asyncIterator in element) {
|
|
93
|
-
// async iterable - lazily resolve
|
|
99
|
+
// any async iterable - lazily resolve
|
|
94
100
|
for await (const children of element)
|
|
95
101
|
yield* toGenerator(children);
|
|
96
102
|
return;
|
|
97
103
|
}
|
|
98
104
|
if (Symbol.iterator in element) {
|
|
99
105
|
// sync iterable
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const result = iterator.next();
|
|
110
|
-
if (result.done)
|
|
111
|
-
break;
|
|
112
|
-
yield* toGenerator(result.value);
|
|
113
|
-
if (--yieldCounter === 0) {
|
|
114
|
-
// yields back to the event loop to send chunks
|
|
115
|
-
// or check if the request has been cancelled
|
|
106
|
+
if (types.isGeneratorObject(element)) {
|
|
107
|
+
// sync generator - lazily resolve, avoids loading all in memory
|
|
108
|
+
const ms = 8;
|
|
109
|
+
let deadline = performance.now() + ms;
|
|
110
|
+
for (const children of element) {
|
|
111
|
+
yield* toGenerator(children);
|
|
112
|
+
if (performance.now() > deadline) {
|
|
113
|
+
// yields back to the event loop if deadline is reached
|
|
114
|
+
// to send chunks or check if the request has been cancelled
|
|
116
115
|
await setImmediate();
|
|
117
|
-
|
|
116
|
+
deadline = performance.now() + ms;
|
|
118
117
|
}
|
|
119
118
|
}
|
|
120
119
|
return;
|
|
121
120
|
}
|
|
122
121
|
// other iterable - array, set, etc.
|
|
123
122
|
// process children in parallel
|
|
124
|
-
const generators =
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
break;
|
|
129
|
-
generators.push(toGenerator(result.value));
|
|
130
|
-
}
|
|
131
|
-
const queue = new Array(generators.length);
|
|
132
|
-
const complete = new Set();
|
|
123
|
+
const generators = Array.from(element, toGenerator);
|
|
124
|
+
const n = generators.length;
|
|
125
|
+
const queue = new Array(n);
|
|
126
|
+
const complete = new Uint8Array(n);
|
|
133
127
|
let current = 0;
|
|
134
128
|
for await (const m of merge(generators)) {
|
|
135
129
|
if (m.result.done) {
|
|
136
|
-
complete
|
|
130
|
+
complete[m.index] = 1;
|
|
137
131
|
if (m.index === current) {
|
|
138
|
-
while (++current <
|
|
132
|
+
while (++current < n) {
|
|
139
133
|
if (queue[current]) {
|
|
140
134
|
// yield whatever is in the next queue even if it hasn't completed yet
|
|
141
135
|
yield queue[current];
|
|
142
136
|
queue[current] = null;
|
|
143
137
|
}
|
|
144
138
|
// if it hasn't completed, stop iterating to the next
|
|
145
|
-
if (!complete
|
|
139
|
+
if (!complete[current])
|
|
146
140
|
break;
|
|
147
141
|
}
|
|
148
142
|
}
|
|
@@ -159,7 +153,7 @@ export async function* toGenerator(element) {
|
|
|
159
153
|
}
|
|
160
154
|
}
|
|
161
155
|
// clear the queue
|
|
162
|
-
yield* queue.filter(
|
|
156
|
+
yield* queue.filter((chunk) => chunk !== null);
|
|
163
157
|
return;
|
|
164
158
|
}
|
|
165
159
|
}
|
|
@@ -198,7 +192,8 @@ export const toStream = (element) => {
|
|
|
198
192
|
return;
|
|
199
193
|
}
|
|
200
194
|
c.enqueue(
|
|
201
|
-
// need to encode for Node JS (ex: during prerendering)
|
|
195
|
+
// need to encode for Node JS (ex: during prerendering) or it will error
|
|
196
|
+
// doesn't seem to be needed for browsers
|
|
202
197
|
// faster than piping through a `TextEncoderStream`
|
|
203
198
|
encoder.encode(result.value.value));
|
|
204
199
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge-async-generators.d.ts","sourceRoot":"","sources":["../../src/jsx/merge-async-generators.ts"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AACH,wBAAuB,KAAK,CAAC,CAAC,EAAE,UAAU,EAAE,cAAc,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE;;;
|
|
1
|
+
{"version":3,"file":"merge-async-generators.d.ts","sourceRoot":"","sources":["../../src/jsx/merge-async-generators.ts"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AACH,wBAAuB,KAAK,CAAC,CAAC,EAAE,UAAU,EAAE,cAAc,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE;;;kBAgCpE"}
|
|
@@ -14,7 +14,9 @@ const next = async (iterator, index) => ({
|
|
|
14
14
|
export async function* merge(generators) {
|
|
15
15
|
const iterators = generators.map((gen) => gen[Symbol.asyncIterator]());
|
|
16
16
|
const promises = new Map();
|
|
17
|
-
|
|
17
|
+
for (let i = 0; i < iterators.length; i++) {
|
|
18
|
+
promises.set(i, next(iterators[i], i));
|
|
19
|
+
}
|
|
18
20
|
let current;
|
|
19
21
|
try {
|
|
20
22
|
while (promises.size > 0) {
|
|
@@ -29,8 +31,12 @@ export async function* merge(generators) {
|
|
|
29
31
|
}
|
|
30
32
|
finally {
|
|
31
33
|
for (const iterator of iterators) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
try {
|
|
35
|
+
iterator.return();
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// could have already returned
|
|
39
|
+
}
|
|
34
40
|
}
|
|
35
41
|
}
|
|
36
42
|
}
|
package/package.json
CHANGED
package/src/app/context.ts
CHANGED
|
@@ -264,9 +264,7 @@ export class Context<P extends Params = Params> {
|
|
|
264
264
|
elements.push(Page, Context.#bodyClose + bodyParts[1]);
|
|
265
265
|
|
|
266
266
|
stream = toStream(
|
|
267
|
-
elements.map((el) =>
|
|
268
|
-
typeof el === "string" ? new Chunk(el, true) : el,
|
|
269
|
-
),
|
|
267
|
+
elements.map((el) => (typeof el === "string" ? Chunk.safe(el) : el)),
|
|
270
268
|
);
|
|
271
269
|
} else {
|
|
272
270
|
// HTML partial - just use the layouts + page
|
package/src/jsx/chunk/index.ts
CHANGED
|
@@ -30,6 +30,18 @@ export class Chunk {
|
|
|
30
30
|
this.value += chunks.join("");
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Wraps `new Chunk("html", true)`.
|
|
35
|
+
*
|
|
36
|
+
* Use to create a new `Chunk` and bypass escaping.
|
|
37
|
+
*
|
|
38
|
+
* @param html Safe string of HTML
|
|
39
|
+
* @returns New _safe_ `Chunk`
|
|
40
|
+
*/
|
|
41
|
+
static safe(html: unknown) {
|
|
42
|
+
return new Chunk(html, true);
|
|
43
|
+
}
|
|
44
|
+
|
|
33
45
|
/**
|
|
34
46
|
* Escapes strings of HTML.
|
|
35
47
|
*
|
package/src/jsx/index.ts
CHANGED
|
@@ -2,7 +2,9 @@ import type { MaybeFunction, MaybePromise } from "../types/index.js";
|
|
|
2
2
|
import { Chunk } from "./chunk/index.js";
|
|
3
3
|
import type { IntrinsicElements as IE } from "./intrinsic-elements.js";
|
|
4
4
|
import { merge } from "./merge-async-generators.js";
|
|
5
|
+
import { performance } from "node:perf_hooks";
|
|
5
6
|
import { setImmediate } from "node:timers/promises";
|
|
7
|
+
import { types } from "node:util";
|
|
6
8
|
|
|
7
9
|
/** ovr JSX namespace */
|
|
8
10
|
export namespace JSX {
|
|
@@ -26,7 +28,7 @@ export namespace JSX {
|
|
|
26
28
|
>;
|
|
27
29
|
}
|
|
28
30
|
|
|
29
|
-
/** Unknown component props
|
|
31
|
+
/** Unknown component props */
|
|
30
32
|
export type Props = Record<string, JSX.Element>;
|
|
31
33
|
|
|
32
34
|
/**
|
|
@@ -52,11 +54,12 @@ const voidElements = new Set([
|
|
|
52
54
|
|
|
53
55
|
/**
|
|
54
56
|
* The main function of the JSX transform cycle, each time JSX is encountered
|
|
55
|
-
* it is passed into this function to be resolved.
|
|
57
|
+
* it is passed into this function to be resolved. This function doesn't need to be
|
|
58
|
+
* called recursively, JSX will be transformed into these `jsx()` function calls.
|
|
56
59
|
*
|
|
57
60
|
* @param tag string or function component
|
|
58
61
|
* @param props object containing all the properties and attributes passed to the element or component
|
|
59
|
-
* @returns
|
|
62
|
+
* @returns `AsyncGenerator` that yields `Chunk`s of HTML
|
|
60
63
|
*/
|
|
61
64
|
export async function* jsx<P extends Props = Props>(
|
|
62
65
|
tag: ((props: P) => JSX.Element) | string,
|
|
@@ -65,17 +68,18 @@ export async function* jsx<P extends Props = Props>(
|
|
|
65
68
|
if (typeof tag === "function") {
|
|
66
69
|
// component or fragment
|
|
67
70
|
yield* toGenerator(tag(props));
|
|
68
|
-
|
|
69
71
|
return;
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
// intrinsic element
|
|
73
|
-
|
|
74
|
-
|
|
75
|
+
// faster to concatenate attributes than to yield them as separate chunks
|
|
75
76
|
let attributes = "";
|
|
76
77
|
|
|
77
|
-
for (const key in
|
|
78
|
-
|
|
78
|
+
for (const key in props) {
|
|
79
|
+
// more memory efficient to skip children instead of destructuring and using ...rest
|
|
80
|
+
if (key === "children") continue;
|
|
81
|
+
|
|
82
|
+
const value = props[key];
|
|
79
83
|
|
|
80
84
|
if (value === true) {
|
|
81
85
|
// just put the key without the value
|
|
@@ -88,13 +92,13 @@ export async function* jsx<P extends Props = Props>(
|
|
|
88
92
|
// otherwise, don't include the attribute
|
|
89
93
|
}
|
|
90
94
|
|
|
91
|
-
yield
|
|
95
|
+
yield Chunk.safe(`<${tag}${attributes}>`);
|
|
92
96
|
|
|
93
97
|
if (voidElements.has(tag)) return;
|
|
94
98
|
|
|
95
|
-
yield* toGenerator(children);
|
|
99
|
+
yield* toGenerator(props.children);
|
|
96
100
|
|
|
97
|
-
yield
|
|
101
|
+
yield Chunk.safe(`</${tag}>`);
|
|
98
102
|
}
|
|
99
103
|
|
|
100
104
|
/**
|
|
@@ -109,7 +113,7 @@ export async function* Fragment(props: { children?: JSX.Element } = {}) {
|
|
|
109
113
|
|
|
110
114
|
/**
|
|
111
115
|
* @param element
|
|
112
|
-
* @yields `Chunk`s of HTML as the `Element` resolves
|
|
116
|
+
* @yields `Chunk`s of HTML as the `Element` resolves
|
|
113
117
|
*/
|
|
114
118
|
export async function* toGenerator(
|
|
115
119
|
element: JSX.Element,
|
|
@@ -127,43 +131,31 @@ export async function* toGenerator(
|
|
|
127
131
|
if (element instanceof Chunk) {
|
|
128
132
|
// already escaped or safe
|
|
129
133
|
yield element;
|
|
130
|
-
|
|
131
134
|
return;
|
|
132
135
|
}
|
|
133
136
|
|
|
134
137
|
if (typeof element === "object") {
|
|
135
138
|
if (Symbol.asyncIterator in element) {
|
|
136
|
-
// async iterable - lazily resolve
|
|
139
|
+
// any async iterable - lazily resolve
|
|
137
140
|
for await (const children of element) yield* toGenerator(children);
|
|
138
|
-
|
|
139
141
|
return;
|
|
140
142
|
}
|
|
141
143
|
|
|
142
144
|
if (Symbol.iterator in element) {
|
|
143
145
|
// sync iterable
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
while (true) {
|
|
157
|
-
const result = iterator.next();
|
|
158
|
-
|
|
159
|
-
if (result.done) break;
|
|
160
|
-
yield* toGenerator(result.value);
|
|
161
|
-
|
|
162
|
-
if (--yieldCounter === 0) {
|
|
163
|
-
// yields back to the event loop to send chunks
|
|
164
|
-
// or check if the request has been cancelled
|
|
146
|
+
if (types.isGeneratorObject(element)) {
|
|
147
|
+
// sync generator - lazily resolve, avoids loading all in memory
|
|
148
|
+
const ms = 8;
|
|
149
|
+
let deadline = performance.now() + ms;
|
|
150
|
+
|
|
151
|
+
for (const children of element) {
|
|
152
|
+
yield* toGenerator(children);
|
|
153
|
+
|
|
154
|
+
if (performance.now() > deadline) {
|
|
155
|
+
// yields back to the event loop if deadline is reached
|
|
156
|
+
// to send chunks or check if the request has been cancelled
|
|
165
157
|
await setImmediate();
|
|
166
|
-
|
|
158
|
+
deadline = performance.now() + ms;
|
|
167
159
|
}
|
|
168
160
|
}
|
|
169
161
|
|
|
@@ -172,24 +164,18 @@ export async function* toGenerator(
|
|
|
172
164
|
|
|
173
165
|
// other iterable - array, set, etc.
|
|
174
166
|
// process children in parallel
|
|
175
|
-
const generators
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
if (result.done) break;
|
|
180
|
-
generators.push(toGenerator(result.value));
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const queue: (Chunk | null)[] = new Array(generators.length);
|
|
184
|
-
const complete = new Set<number>();
|
|
167
|
+
const generators = Array.from(element, toGenerator);
|
|
168
|
+
const n = generators.length;
|
|
169
|
+
const queue = new Array<Chunk | null>(n);
|
|
170
|
+
const complete = new Uint8Array(n);
|
|
185
171
|
let current = 0;
|
|
186
172
|
|
|
187
173
|
for await (const m of merge(generators)) {
|
|
188
174
|
if (m.result.done) {
|
|
189
|
-
complete
|
|
175
|
+
complete[m.index] = 1;
|
|
190
176
|
|
|
191
177
|
if (m.index === current) {
|
|
192
|
-
while (++current <
|
|
178
|
+
while (++current < n) {
|
|
193
179
|
if (queue[current]) {
|
|
194
180
|
// yield whatever is in the next queue even if it hasn't completed yet
|
|
195
181
|
yield queue[current]!;
|
|
@@ -197,7 +183,7 @@ export async function* toGenerator(
|
|
|
197
183
|
}
|
|
198
184
|
|
|
199
185
|
// if it hasn't completed, stop iterating to the next
|
|
200
|
-
if (!complete
|
|
186
|
+
if (!complete[current]) break;
|
|
201
187
|
}
|
|
202
188
|
}
|
|
203
189
|
} else if (m.index === current) {
|
|
@@ -210,8 +196,7 @@ export async function* toGenerator(
|
|
|
210
196
|
}
|
|
211
197
|
|
|
212
198
|
// clear the queue
|
|
213
|
-
yield* queue.filter(
|
|
214
|
-
|
|
199
|
+
yield* queue.filter((chunk) => chunk !== null);
|
|
215
200
|
return;
|
|
216
201
|
}
|
|
217
202
|
}
|
|
@@ -258,7 +243,8 @@ export const toStream = (element: JSX.Element) => {
|
|
|
258
243
|
}
|
|
259
244
|
|
|
260
245
|
c.enqueue(
|
|
261
|
-
// need to encode for Node JS (ex: during prerendering)
|
|
246
|
+
// need to encode for Node JS (ex: during prerendering) or it will error
|
|
247
|
+
// doesn't seem to be needed for browsers
|
|
262
248
|
// faster than piping through a `TextEncoderStream`
|
|
263
249
|
encoder.encode(result.value.value),
|
|
264
250
|
);
|
|
@@ -16,9 +16,9 @@ export async function* merge<T>(generators: AsyncGenerator<T, void>[]) {
|
|
|
16
16
|
const iterators = generators.map((gen) => gen[Symbol.asyncIterator]());
|
|
17
17
|
const promises = new Map<number, ReturnType<typeof next<T, void>>>();
|
|
18
18
|
|
|
19
|
-
iterators.
|
|
20
|
-
promises.set(
|
|
21
|
-
|
|
19
|
+
for (let i = 0; i < iterators.length; i++) {
|
|
20
|
+
promises.set(i, next(iterators[i]!, i));
|
|
21
|
+
}
|
|
22
22
|
|
|
23
23
|
let current: Awaited<ReturnType<typeof next>>;
|
|
24
24
|
|
|
@@ -37,8 +37,11 @@ export async function* merge<T>(generators: AsyncGenerator<T, void>[]) {
|
|
|
37
37
|
}
|
|
38
38
|
} finally {
|
|
39
39
|
for (const iterator of iterators) {
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
try {
|
|
41
|
+
iterator.return();
|
|
42
|
+
} catch {
|
|
43
|
+
// could have already returned
|
|
44
|
+
}
|
|
42
45
|
}
|
|
43
46
|
}
|
|
44
47
|
}
|