diff --git a/asyncblock/asyncblock-tests.ts b/asyncblock/asyncblock-tests.ts index 57328e13a5..35707e81ec 100644 --- a/asyncblock/asyncblock-tests.ts +++ b/asyncblock/asyncblock-tests.ts @@ -117,6 +117,55 @@ asyncblock(function(flow) { })(); +// Parallel task rate limiting +(function() { + +asyncblock(function(flow) { + flow.queue(function(callback: Function) { + setTimeout(callback, 1000); + }); + + flow.wait(); //This will wait for about a second +}); + +asyncblock(function(flow) { + flow.maxParallel = 2; + + process.nextTick(function(){ + flow.queue(function(callback: Function) { + setTimeout(callback, 1000); + }); + + flow.queue(function(callback: Function) { + setTimeout(callback, 2000); + }); + + flow.queue(function(callback: Function) { + setTimeout(callback, 3000); + }); + + flow.doneAdding(); + }); + + flow.forceWait(); +}); + +asyncblock(function(flow) { + setTimeout(flow.callback(), 1000); + + flow.queue(function(callback: Function) { + setTimeout(callback, 1000); + }); + flow.queue((callback: (err: any, res: string) => void) => { + callback(null, ''); + }); + + flow.wait(); +}); + +})(); + + // Task timeouts (function() { diff --git a/asyncblock/asyncblock.d.ts b/asyncblock/asyncblock.d.ts index d1cd5e0052..ef49640c36 100644 --- a/asyncblock/asyncblock.d.ts +++ b/asyncblock/asyncblock.d.ts @@ -11,23 +11,33 @@ declare module "asyncblock" { export function nostack(f: (flow: asyncblock.IFlow) => void, callback?: (err: any, res: T) => void): void; export interface IFlow { - add(responseFormat?: string[]): any; - add(key: string, responseFormat?: string[]): any; - add(key: number, responseFormat?: string[]): any; - add(options: IFlowOptions): any; - callback(responseFormat?: string[]): any; - callback(key: string, responseFormat?: string[]): any; - callback(key: number, responseFormat?: string[]): any; - callback(options: IFlowOptions): any; - sync(f: void): T; + add(responseFormat?: string[]): IExecuteFunction; + add(key: string, responseFormat?: string[]): IExecuteFunction; + add(key: number, responseFormat?: string[]): IExecuteFunction; + add(options: IFlowOptions): IExecuteFunction; + callback(responseFormat?: string[]): IExecuteFunction; + callback(key: string, responseFormat?: string[]): IExecuteFunction; + callback(key: number, responseFormat?: string[]): IExecuteFunction; + callback(options: IFlowOptions): IExecuteFunction; wait(key?: string): T; wait(key?: number): T; get(key: string): T; - set(key: string, responseFormat?: string[]): any; - set(options: IFlowOptions): any; + set(key: string, responseFormat?: string[]): IExecuteFunction; + set(options: IFlowOptions): IExecuteFunction; del(key: string): void; + sync(task: any): T; + queue(toExecute: IExecuteFunction): void; + queue(key: string, toExecute: IExecuteFunction): void; + queue(key: number, toExecute: IExecuteFunction): void; + queue(responseFormat: string[], toExecute: IExecuteFunction): void; + queue(key: string, responseFormat: string[], toExecute: IExecuteFunction): void; + queue(key: number, responseFormat: string[], toExecute: IExecuteFunction): void; + queue(options: IFlowOptions, toExecute: IExecuteFunction): void; + doneAdding(): void; + forceWait(): T; + maxParallel: number; errorCallback: (err: any) => void; taskTimeout: number; @@ -41,7 +51,19 @@ declare module "asyncblock" { timeout?: number; timeoutIsError?: boolean; dontWait?: boolean; - firstArgIsError?: boolean; // default false + firstArgIsError?: boolean; // default true + } + + export interface IExecuteFunction { + (err: any, res1: T1, res2: T2, res3: T3): any; + (err: any, res1: T1, res2: T2): any; + (err: any, res: T): any; + (err: any): any; + + // firstArgIsError === false + (res1: T1, res2: T2, res3: T3): any; + (res1: T1, res2: T2): any; + (res: T): any; } }