Initial commit

This commit is contained in:
Honey committed 2025-11-15 18:54:32 +01:00
commit f1548f10cc
7585 files changed
+1411120

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
/* IMPORT */
import {Exception, FN} from '../types';
import RetryfyQueue from './retryify_queue';
/* RETRYIFY */
const retryifyAsync = <T extends FN> ( fn: T, isRetriableError: FN<[Exception], boolean | void> ): FN<[number], T> => {
return function ( timestamp: number ) {
return function attempt () {
return RetryfyQueue.schedule ().then ( cleanup => {
return fn.apply ( undefined, arguments ).then ( result => {
cleanup ();
return result;
}, error => {
cleanup ();
if ( Date.now () >= timestamp ) throw error;
if ( isRetriableError ( error ) ) {
const delay = Math.round ( 100 + ( 400 * Math.random () ) ),
delayPromise = new Promise ( resolve => setTimeout ( resolve, delay ) );
return delayPromise.then ( () => attempt.apply ( undefined, arguments ) );
}
throw error;
});
});
} as T;
};
};
const retryifySync = <T extends FN> ( fn: T, isRetriableError: FN<[Exception], boolean | void> ): FN<[number], T> => {
return function ( timestamp: number ) {
return function attempt () {
try {
return fn.apply ( undefined, arguments );
} catch ( error ) {
if ( Date.now () > timestamp ) throw error;
if ( isRetriableError ( error ) ) return attempt.apply ( undefined, arguments );
throw error;
}
} as T;
};
};
/* EXPORT */
export {retryifyAsync, retryifySync};