mirror of
https://github.com/koush/scrypted.git
synced 2026-03-02 01:02:57 +00:00
31 lines
772 B
TypeScript
31 lines
772 B
TypeScript
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 function timeoutPromise<T>(timeout: number, promise: Promise<T>): Promise<T> {
|
|
return new Promise<T>((resolve, reject) => {
|
|
setTimeout(() => reject(new Error('timed out')), timeout);
|
|
promise.then(resolve);
|
|
promise.catch(reject);
|
|
})
|
|
}
|