snapshot: add some basic blurs and error logging to snapshots

This commit is contained in:
Koushik Dutta
2022-02-23 21:49:39 -08:00
parent 544a9d8661
commit b690cd71cc
4 changed files with 1530 additions and 33 deletions

View File

@@ -0,0 +1,36 @@
export interface RefreshPromise<T> {
promise: Promise<T>;
cacheDuration: number;
}
export function singletonPromise<T>(rp: RefreshPromise<T>, method: () => Promise<T>) {
if (rp?.promise)
return rp;
const promise = method();
if (!rp) {
rp = {
promise,
cacheDuration: 0,
}
}
else {
rp.promise = promise;
}
promise.finally(() => setTimeout(() => rp.promise = undefined, rp.cacheDuration));
return rp;
}
export class TimeoutError extends Error {
constructor() {
super('Operation Timed Out');
}
}
export function timeoutPromise<T>(timeout: number, promise: Promise<T>): Promise<T> {
return new Promise<T>((resolve, reject) => {
setTimeout(() => reject(new TimeoutError()), timeout);
promise.then(resolve);
promise.catch(reject);
})
}