Refactor models to use bundleAsync and lock regions (#171)

This commit is contained in:
Paul Frazee
2023-02-08 11:57:40 -06:00
committed by GitHub
parent 82ea907e8f
commit c2b7796317
11 changed files with 378 additions and 444 deletions
+24
View File
@@ -0,0 +1,24 @@
type BundledFn<Args extends readonly unknown[], Res> = (
...args: Args
) => Promise<Res>
/**
* A helper which ensures that multiple calls to an async function
* only produces one in-flight request at a time.
*/
export function bundleAsync<Args extends readonly unknown[], Res>(
fn: BundledFn<Args, Res>,
): BundledFn<Args, Res> {
let promise: Promise<Res> | undefined
return async (...args) => {
if (promise) {
return promise
}
promise = fn(...args)
try {
return await promise
} finally {
promise = undefined
}
}
}