-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(async): add
repeatable
helper function
- Loading branch information
1 parent
85fd83b
commit b0f587f
Showing
8 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
export function repeatable<T>(iterable: AsyncIterable<T>): AsyncIterable<T> { | ||
const cache: T[] = []; | ||
let buildingCache: Promise<void> | undefined = undefined; | ||
let pendingResolvers: ((value: T) => void)[] = []; | ||
let finished = false; | ||
|
||
return { | ||
[Symbol.asyncIterator]: async function* () { | ||
yield* cache; | ||
|
||
if (!finished) { | ||
if (!buildingCache) { | ||
buildingCache = (async () => { | ||
try { | ||
for await (const item of iterable) { | ||
cache.push(item); | ||
pendingResolvers.forEach((resolve) => resolve(item)); | ||
pendingResolvers = []; | ||
} | ||
} finally { | ||
finished = true; | ||
} | ||
})(); | ||
} | ||
} | ||
let index = cache.length; | ||
while (!finished || index < cache.length) { | ||
if (index < cache.length) { | ||
yield cache[index++]; | ||
} else { | ||
const nextItem = await new Promise<T>((resolve) => { | ||
pendingResolvers.push(resolve); | ||
}); | ||
yield nextItem; | ||
index++; | ||
} | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { test } from "@cross/test"; | ||
import { delay } from "@std/async/delay"; | ||
import { assertEquals } from "@std/assert"; | ||
import { repeatable } from "./repeatable.ts"; | ||
|
||
async function* delayedGenerator(sideEffect?: () => void) { | ||
yield 1; | ||
await delay(100); | ||
yield 2; | ||
await delay(100); | ||
yield 3; | ||
sideEffect?.(); | ||
} | ||
|
||
await test("repeatable should return the same sequence on multiple iterations", async () => { | ||
const input = delayedGenerator(); | ||
const it = repeatable(input); | ||
|
||
const result1 = await Array.fromAsync(it); | ||
const result2 = await Array.fromAsync(it); | ||
|
||
assertEquals(result1, [1, 2, 3], "First iteration"); | ||
assertEquals(result2, [1, 2, 3], "First iteration"); | ||
}); | ||
|
||
await test("repeatable should call internal iterator only once", async () => { | ||
let called = 0; | ||
const input = delayedGenerator(() => called++); | ||
const it = repeatable(input); | ||
|
||
const result1 = await Array.fromAsync(it); | ||
const result2 = await Array.fromAsync(it); | ||
|
||
assertEquals(result1, [1, 2, 3], "First iteration"); | ||
assertEquals(result2, [1, 2, 3], "First iteration"); | ||
assertEquals(called, 1, "Internal iterator called only once"); | ||
}); | ||
|
||
await test("repeatable should work correctly when consumed partially and then fully", async () => { | ||
const input = delayedGenerator(); | ||
const it = repeatable(input); | ||
|
||
const result1: number[] = []; | ||
const firstIter = it[Symbol.asyncIterator](); | ||
|
||
result1.push((await firstIter.next()).value); // 1 | ||
|
||
const result2 = await Array.fromAsync(it); | ||
|
||
result1.push((await firstIter.next()).value); // 2 | ||
result1.push((await firstIter.next()).value); // 3 | ||
|
||
assertEquals(result1, [1, 2, 3], "First iteration"); | ||
assertEquals(result2, [1, 2, 3], "First iteration"); | ||
}); | ||
|
||
await test("repeatable should cache values and return them immediately on subsequent iterations", async () => { | ||
const input = delayedGenerator(); | ||
const it = repeatable(input); | ||
|
||
const start = performance.now(); | ||
const result1 = await Array.fromAsync(it); | ||
const end1 = performance.now(); | ||
const timeTaken1 = end1 - start; | ||
|
||
const start2 = performance.now(); | ||
const result2 = await Array.fromAsync(it); | ||
const end2 = performance.now(); | ||
const timeTaken2 = end2 - start2; | ||
|
||
assertEquals(result1, [1, 2, 3], "First iteration"); | ||
assertEquals(result2, [1, 2, 3], "Second iteration"); | ||
|
||
console.debug("Time taken for first consume:", timeTaken1); | ||
console.debug("Time taken for second consume (with cache):", timeTaken2); | ||
|
||
if (timeTaken2 > timeTaken1 / 10) { | ||
throw new Error( | ||
"Second consume took too long, cache might not be working.", | ||
); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters