ovr 2.0.2 → 2.0.4
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.js +11 -5
- package/dist/jsx/index.js +47 -28
- package/dist/jsx/merge-async-iterables.d.ts +7 -6
- package/dist/jsx/merge-async-iterables.js +21 -14
- package/package.json +1 -1
package/dist/app/context.js
CHANGED
|
@@ -21,9 +21,9 @@ export class Context {
|
|
|
21
21
|
*
|
|
22
22
|
* @example { slug: "my-post" }
|
|
23
23
|
*/
|
|
24
|
-
params
|
|
24
|
+
params; // set after match
|
|
25
25
|
/** The matched `Route` instance. */
|
|
26
|
-
route
|
|
26
|
+
route; // set after match
|
|
27
27
|
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
|
|
28
28
|
body = null;
|
|
29
29
|
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
|
|
@@ -155,11 +155,17 @@ export class Context {
|
|
|
155
155
|
}
|
|
156
156
|
elements[3] = bodyParts[0];
|
|
157
157
|
elements.push(Page, bodyClose + bodyParts[1]);
|
|
158
|
+
const gen = toGenerator(elements);
|
|
158
159
|
this.html(new ReadableStream({
|
|
159
|
-
async
|
|
160
|
-
|
|
160
|
+
async pull(c) {
|
|
161
|
+
const { value, done } = await gen.next();
|
|
162
|
+
if (done)
|
|
163
|
+
c.close();
|
|
164
|
+
else
|
|
161
165
|
c.enqueue(value);
|
|
162
|
-
|
|
166
|
+
},
|
|
167
|
+
cancel() {
|
|
168
|
+
gen.return();
|
|
163
169
|
},
|
|
164
170
|
}).pipeThrough(new TextEncoderStream()), status);
|
|
165
171
|
}
|
package/dist/jsx/index.js
CHANGED
|
@@ -82,40 +82,59 @@ export async function* toGenerator(element) {
|
|
|
82
82
|
yield* toGenerator(children);
|
|
83
83
|
}
|
|
84
84
|
else if (Symbol.iterator in element) {
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
85
|
+
const iterator = element[Symbol.iterator]();
|
|
86
|
+
if (typeof iterator.next === "function" &&
|
|
87
|
+
typeof iterator.throw === "function" &&
|
|
88
|
+
typeof iterator.return === "function") {
|
|
89
|
+
// sync generator
|
|
90
|
+
// process lazily - avoids loading all in memory
|
|
91
|
+
while (true) {
|
|
92
|
+
const result = iterator.next();
|
|
93
|
+
if (result.done)
|
|
94
|
+
break;
|
|
95
|
+
yield* toGenerator(result.value);
|
|
96
|
+
}
|
|
89
97
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
98
|
+
else {
|
|
99
|
+
// other iterable - array, set, etc.
|
|
100
|
+
// process children in parallel
|
|
101
|
+
const generators = [];
|
|
102
|
+
while (true) {
|
|
103
|
+
const result = iterator.next();
|
|
104
|
+
if (result.done)
|
|
105
|
+
break;
|
|
106
|
+
generators.push(toGenerator(result.value));
|
|
107
|
+
}
|
|
108
|
+
let queue = new Array(generators.length).fill("");
|
|
109
|
+
let current = 0;
|
|
110
|
+
const complete = new Set();
|
|
111
|
+
for await (const { index, result } of merge(generators)) {
|
|
112
|
+
if (result.done) {
|
|
113
|
+
complete.add(index);
|
|
114
|
+
if (index === current) {
|
|
115
|
+
while (++current < generators.length) {
|
|
116
|
+
if (queue[current]) {
|
|
117
|
+
// yield whatever is in the next queue even if it hasn't completed yet
|
|
118
|
+
yield queue[current];
|
|
119
|
+
queue[current] = "";
|
|
120
|
+
}
|
|
121
|
+
// if it hasn't completed, stop iterating to the next
|
|
122
|
+
if (!complete.has(current))
|
|
123
|
+
break;
|
|
102
124
|
}
|
|
103
|
-
// if it hasn't completed, stop iterating to the next
|
|
104
|
-
if (!complete.has(current))
|
|
105
|
-
break;
|
|
106
125
|
}
|
|
107
126
|
}
|
|
127
|
+
else if (index === current) {
|
|
128
|
+
yield result.value; // stream the current value directly
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
queue[index] += result.value; // queue the value for later
|
|
132
|
+
}
|
|
108
133
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
else {
|
|
113
|
-
queue[index] += result.value; // queue the value for later
|
|
114
|
-
}
|
|
134
|
+
queue = queue.join("");
|
|
135
|
+
if (queue)
|
|
136
|
+
yield queue; // clear the queue
|
|
115
137
|
}
|
|
116
|
-
queue = queue.join("");
|
|
117
|
-
if (queue)
|
|
118
|
-
yield queue; // clear the queue
|
|
119
138
|
}
|
|
120
139
|
else {
|
|
121
140
|
yield JSON.stringify(element); // avoids things like [object Object]
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Merges `
|
|
3
|
-
* The return of each `
|
|
2
|
+
* Merges `AsyncGenerator[]` into a single `AsyncGenerator`, resolving all in parallel.
|
|
3
|
+
* The return of each `AsyncGenerator` is yielded from the generator with `done: true`.
|
|
4
4
|
*
|
|
5
5
|
* Adapted from [stack overflow answers](https://stackoverflow.com/questions/50585456).
|
|
6
6
|
*
|
|
7
|
-
* @param
|
|
8
|
-
* @
|
|
7
|
+
* @param generators Resolved in parallel.
|
|
8
|
+
* @param returnValue Passed into each `iterator.return()`
|
|
9
|
+
* @yields `IteratorResult` and `index` of the resolved generator.
|
|
9
10
|
*/
|
|
10
|
-
export declare function merge<T
|
|
11
|
+
export declare function merge<T>(generators: AsyncGenerator<T, void>[]): AsyncGenerator<{
|
|
11
12
|
index: number;
|
|
12
|
-
result: IteratorResult<T,
|
|
13
|
+
result: IteratorResult<T, void>;
|
|
13
14
|
}, void, unknown>;
|
|
@@ -4,27 +4,34 @@ const next = async (iterator, index) => ({
|
|
|
4
4
|
result: await iterator.next(),
|
|
5
5
|
});
|
|
6
6
|
/**
|
|
7
|
-
* Merges `
|
|
8
|
-
* The return of each `
|
|
7
|
+
* Merges `AsyncGenerator[]` into a single `AsyncGenerator`, resolving all in parallel.
|
|
8
|
+
* The return of each `AsyncGenerator` is yielded from the generator with `done: true`.
|
|
9
9
|
*
|
|
10
10
|
* Adapted from [stack overflow answers](https://stackoverflow.com/questions/50585456).
|
|
11
11
|
*
|
|
12
|
-
* @param
|
|
13
|
-
* @
|
|
12
|
+
* @param generators Resolved in parallel.
|
|
13
|
+
* @param returnValue Passed into each `iterator.return()`
|
|
14
|
+
* @yields `IteratorResult` and `index` of the resolved generator.
|
|
14
15
|
*/
|
|
15
|
-
export async function* merge(
|
|
16
|
-
const iterators =
|
|
16
|
+
export async function* merge(generators) {
|
|
17
|
+
const iterators = generators.map((gen) => gen[Symbol.asyncIterator]());
|
|
17
18
|
const promises = iterators.map(next);
|
|
18
19
|
let remaining = promises.length;
|
|
19
20
|
let current;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
try {
|
|
22
|
+
while (remaining) {
|
|
23
|
+
yield (current = await Promise.race(promises));
|
|
24
|
+
if (current.result.done) {
|
|
25
|
+
promises[current.index] = never;
|
|
26
|
+
remaining--;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
promises[current.index] = next(iterators[current.index], current.index);
|
|
30
|
+
}
|
|
28
31
|
}
|
|
29
32
|
}
|
|
33
|
+
finally {
|
|
34
|
+
for (const iter of iterators)
|
|
35
|
+
iter.return().catch(); // could have already returned
|
|
36
|
+
}
|
|
30
37
|
}
|