d4c-queue
Version:
A task queue executes tasks sequentially or concurrently. Wrap an async/promise-returning/sync function as a queue-ready async function for easy reusing. Support passing arguments/getting return value, @synchronized/@concurrent decorator, Node.js/Browser.
102 lines (101 loc) • 3.83 kB
TypeScript
declare type UnwrapPromise<T> = T extends Promise<infer U> ? U : T extends (...args: any) => Promise<infer U> ? U : T extends (...args: any) => infer U ? U : T;
declare type IAnyFn = (...args: any[]) => Promise<any> | any;
declare type MethodDecoratorType = (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
export declare enum ErrMsg {
InstanceInvalidTag = "instanceInvalidTag: it should be string/symbol/undefined",
InvalidDecoratorOption = "not valid option when using decorators",
InvalidQueueConcurrency = "invalidQueueConcurrency",
InvalidQueueTag = "invalidQueueTag",
InvalidClassDecoratorParameter = "invalidClassDecoratorParameter",
TwoDecoratorsIncompatible = "TwoDecoratorsInCompatible",
ClassAndMethodDecoratorsIncompatible = "ClassAndMethodDecoratorsIncompatible",
MissingThisDueBindIssue = "missingThisDueBindIssue",
QueueIsFull = "QueueIsFull"
}
export declare class PreviousTaskError extends Error {
constructor(message: any);
}
/**
* Class decorator to setup concurrency for queues
* @param queuesParam a array of each queue parameter
*/
export declare function QConcurrency(queuesParam: Array<{
limit: number;
tag?: string | symbol;
isStatic?: boolean;
}>): ClassDecorator;
/**
* Static and instance method decorator. Default concurrency = Infinity.
* usage example:
* ```typescript
* @concurrent
* async fetchData() {}
* // or
* @concurrent({ tag: 'world', inheritPreErr: true, noBlockCurr: true })
* static async fetchData(url: string) {}
* ```
* */
export declare function concurrent(target: any, propertyKey: string, descriptor: PropertyDescriptor): void;
export declare function concurrent(option?: {
tag?: string | symbol;
inheritPreErr?: boolean;
noBlockCurr?: boolean;
dropWhenReachLimit?: boolean;
}): MethodDecoratorType;
/**
* Static and instance method decorator. Default concurrency = 1 for lock.
* usage example:
* ```typescript
* @synchronize
* async connect() {}
* // or
* @synchronized({ tag: 'world', inheritPreErr: true, noBlockCurr: true })
* static async staticMethod(text: string) {}
* ```
* */
export declare function synchronized(target: any, propertyKey: string, descriptor: PropertyDescriptor): void;
export declare function synchronized(option?: {
tag?: string | symbol;
inheritPreErr?: boolean;
noBlockCurr?: boolean;
}): MethodDecoratorType;
export declare class D4C {
private queues;
private defaultConcurrency;
/**
* Default concurrency is 1. Omitting tag means it is for default queue.
* If you specify concurrency limit for some tag queue,
* this instance will not use that tag queue by default.
*/
constructor(queuesParam?: Array<{
concurrency: {
tag?: string | symbol;
limit?: number;
};
}>);
/**
* @param option tag is optional for specific queue. omitting is for default queue
* @param option.limit is limit of concurrency and should be >= 1
*/
setConcurrency(queuesParam: Array<{
tag?: string | symbol;
limit: number;
}>): void;
private _setConcurrency;
/** It wraps original function for queue ready and executes it*/
apply<T extends IAnyFn>(func: T, option?: {
tag?: string | symbol;
inheritPreErr?: boolean;
noBlockCurr?: boolean;
dropWhenReachLimit?: boolean;
args?: Parameters<typeof func>;
}): Promise<UnwrapPromise<typeof func>>;
/** It wraps original function for queue ready */
wrap<T extends IAnyFn>(func: T, option?: {
tag?: string | symbol;
inheritPreErr?: boolean;
noBlockCurr?: boolean;
dropWhenReachLimit?: boolean;
}): (...args: Parameters<typeof func>) => Promise<UnwrapPromise<typeof func>>;
}
export {};