Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / shared / lib / apiHandlers.ts
blob52e181e65f7b424f53e2b449ed6a315253d66490
1 export const createOnceHandler = <Argument extends any, ReturnValue extends any>(
2     createPromise: (...args: Argument[]) => Promise<ReturnValue>
3 ) => {
4     let promise: Promise<ReturnValue> | undefined;
6     const clear = () => {
7         promise = undefined;
8     };
10     return (...args: Argument[]): Promise<ReturnValue> => {
11         if (promise) {
12             return promise;
13         }
15         promise = createPromise(...args)
16             .then((result: ReturnValue) => {
17                 clear();
18                 return result;
19             })
20             .catch((e: any) => {
21                 clear();
22                 throw e;
23             });
24         return promise;
25     };