@hazae41/piscine
Version:
Create async pools with automatic retry
61 lines (59 loc) • 1.92 kB
TypeScript
type Looper<T> = (index: number) => Promise<T>;
declare class TooManyRetriesError extends Error {
#private;
readonly name: string;
constructor(options?: ErrorOptions);
static from(cause: unknown): TooManyRetriesError;
}
type Looped<T> = Cancel<T> | Retry<T> | Skip<T>;
declare namespace Looped {
type Infer<T> = Cancel.Infer<T> | Skip.Infer<T> | Retry.Infer<T>;
type Inner<T> = Cancel.Inner<T> | Skip.Inner<T> | Retry.Inner<T>;
}
declare class Cancel<T> {
readonly inner: T;
constructor(inner: T);
static new<T>(inner: T): Cancel<T>;
isCancel(): this is Cancel<T>;
isRetry(): false;
isSkip(): false;
}
declare namespace Cancel {
type Infer<T> = Cancel<Inner<T>>;
type Inner<T> = T extends Cancel<infer Inner> ? Inner : never;
}
declare class Retry<T> {
readonly inner: T;
constructor(inner: T);
static new<T>(inner: T): Retry<T>;
isCancel(): false;
isRetry(): this is Retry<T>;
isSkip(): false;
}
declare namespace Retry {
type Infer<T> = Retry<Inner<T>>;
type Inner<T> = T extends Retry<infer Inner> ? Inner : never;
function runSync<T>(f: () => T): T;
function run<T>(f: () => Promise<T>): Promise<T>;
}
declare class Skip<T> {
readonly inner: T;
constructor(inner: T);
static new<T>(inner: T): Skip<T>;
isCancel(): false;
isRetry(): false;
isSkip(): this is Skip<T>;
}
declare namespace Skip {
type Infer<T> = Skip<Inner<T>>;
type Inner<T> = T extends Skip<infer Inner> ? Inner : never;
function runSync<T>(f: () => T): T;
function run<T>(f: () => Promise<T>): Promise<T>;
}
interface LoopOptions {
readonly init?: number;
readonly base?: number;
readonly max?: number;
}
declare function loopOrThrow<T>(looper: Looper<T>, options?: LoopOptions): Promise<T>;
export { Cancel, type LoopOptions, Looped, type Looper, Retry, Skip, TooManyRetriesError, loopOrThrow };