diff --git a/.gitignore b/.gitignore index c2658d7..a5199f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +.idea \ No newline at end of file diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..b774eb9 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,87 @@ +import {EventEmitter} from "events"; + +export interface BackoffStrategyOptions { + initialDelay?: number; + maxDelay?: number; + randomisationFactor?: number +} + +abstract class BackoffStrategy { + constructor(options?: BackoffStrategyOptions) + + getMaxDelay(): number; + + getInitialDelay(): number; + + next(); + + reset(); +} + +export class FibonacciStrategy extends BackoffStrategy { + +} + +export interface ExponentialStrategyOptions extends BackoffStrategyOptions { + factor?: number +} + +export class ExponentialStrategy extends BackoffStrategy { + constructor(options?: ExponentialStrategyOptions); + + public static DEFAULT_FACTOR: number; +} + +export class Backoff extends EventEmitter { + constructor(backoffStrategy: BackoffStrategy); + + failAfter(maxNumberOfRetry: number); + + backoff(err?: Error); + + reset(); +} + + +export class FunctionCall extends EventEmitter { + + constructor(fn: Function, args: any[], callback: Function); + + isPending(): boolean; + + isRunning(): boolean; + + isCompleted(): boolean; + + isAborted(): boolean; + + setStrategy(strategy: BackoffStrategy): this; + + retryIf(retryPredicate: (err: Error) => boolean): this; + + getLastResult(): any; + + getNumRetries(): number; + + failAfter(maxNumberOfRetry: number): this; + + abort(); + + start(backoffFactory?: (strategy: BackoffStrategy) => Backoff); +} + +export function fibonacci(options?: BackoffStrategyOptions): Backoff; + +export function exponential(options?: ExponentialStrategyOptions): Backoff; + +// Have to be done that way until this gets solved https://github.com/Microsoft/TypeScript/issues/1360 +export function call(fn: Function, arg1: any, callback: Function): FunctionCall; +export function call(fn: Function, arg1: any, arg2: any, callback: Function): FunctionCall; +export function call(fn: Function, arg1: any, arg2: any, arg3: any, callback: Function): FunctionCall; +export function call(fn: Function, arg1: any, arg2: any, arg3: any, arg4: any, callback: Function): FunctionCall; +export function call(fn: Function, arg1: any, arg2: any, arg3: any, arg4: any, arg5: any, callback: Function): FunctionCall; +export function call(fn: Function, arg1: any, arg2: any, arg3: any, arg4: any, arg5: any, arg6: any, callback: Function): FunctionCall; +export function call(fn: Function, arg1: any, arg2: any, arg3: any, arg4: any, arg5: any, arg6: any, arg7: any, callback: Function): FunctionCall; +export function call(fn: Function, arg1: any, arg2: any, arg3: any, arg4: any, arg5: any, arg6: any, arg7: any, arg8: any, callback: Function): FunctionCall; +export function call(fn: Function, arg1: any, arg2: any, arg3: any, arg4: any, arg5: any, arg6: any, arg7: any, arg8: any, arg9: any, callback: Function): FunctionCall; +export function call(fn: Function, arg1: any, arg2: any, arg3: any, arg4: any, arg5: any, arg6: any, arg7: any, arg8: any, arg9: any, arg10: any, callback: Function): FunctionCall; \ No newline at end of file diff --git a/package.json b/package.json index b6d6cb6..fb73119 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ }, "devDependencies": { "sinon": "1.10", - "nodeunit": "0.9" + "nodeunit": "0.9", + "@types/node": "^7.0.12" }, "scripts": { "docco" : "docco lib/*.js lib/strategy/* index.js",