|
|
@@ -1,9 +1,7 @@
|
|
|
-export function restartableAsync<T>(
|
|
|
- iter: AsyncIterable<T>
|
|
|
-): () => AsyncIterable<T> {
|
|
|
+export function restartableAsync<T>(iter: AsyncIterable<T>): AsyncIterable<T> {
|
|
|
// buffer stores all items that have been previously consumed.
|
|
|
const buffer: T[] = [];
|
|
|
- return async function* () {
|
|
|
+ const gen = async function* () {
|
|
|
// index of the next item in the buffer to yield.
|
|
|
let i = 0;
|
|
|
// produce all items previously consumed by other iterators.
|
|
|
@@ -14,7 +12,7 @@ export function restartableAsync<T>(
|
|
|
for await (const item of iter) {
|
|
|
// this is a little subtle, but other concurrent iterators may have
|
|
|
// consumed and buffered items while we were waiting. So we need to put
|
|
|
- // our new item in the back of the buffer and yield from where we preiously
|
|
|
+ // our new item in the back of the buffer and yield from where we previously
|
|
|
// left off.
|
|
|
buffer.push(item);
|
|
|
for (; i < buffer.length; i++) {
|
|
|
@@ -22,4 +20,8 @@ export function restartableAsync<T>(
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
+
|
|
|
+ return {
|
|
|
+ [Symbol.asyncIterator]: () => gen(),
|
|
|
+ };
|
|
|
}
|