mirror of
https://github.com/koush/scrypted.git
synced 2026-03-02 01:02:57 +00:00
snapshot: add some basic blurs and error logging to snapshots
This commit is contained in:
36
common/src/promise-utils.ts
Normal file
36
common/src/promise-utils.ts
Normal 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);
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user