@lzptec/concurrency
Version:
A Lightweight concurrency manager
201 lines (196 loc) • 7.11 kB
text/typescript
type RunnableTask<A, B> = (...args: A[]) => Promise<B> | B;
type Task<A, B> = (item: A) => Promise<B> | B;
type Input<A> = AsyncIterable<A | Promise<A>> | Iterable<A | Promise<A>>;
type Group<T> = {
[k: string]: T[];
};
interface BatchCommonOptions {
/**
* Batch size
*/
batchSize: number;
/**
* Interval between batches(in MS)
*/
batchInterval?: number;
}
interface BatchTaskOptions<A, B> extends BatchCommonOptions {
/**
* Arguments to pass to the task for each call.
*/
input: Input<A>;
/**
* The task to run for each item.
*/
task: Task<A, B>;
}
interface BatchPredicateOptions<A> extends BatchCommonOptions {
/**
* Arguments to pass to the predicate for each call.
*/
input: Input<A>;
/**
* The predicate function is called one time for each element in the `input`.
*/
predicate: Task<A, boolean>;
}
interface ConcurrencyCommonOptions {
/**
* Max concurrency
*/
maxConcurrency: number;
/**
* Interval between jobs(in MS)
*/
concurrencyInterval?: number;
}
interface ConcurrencyTaskOptions<A, B> extends ConcurrencyCommonOptions {
/**
* Arguments to pass to the task for each call.
*/
input: Input<A>;
/**
* The task to run for each item.
*/
task: Task<A, B>;
}
interface ConcurrencyPredicateOptions<A> extends ConcurrencyCommonOptions {
/**
* Arguments to pass to the predicate for each call.
*/
input: Input<A>;
/**
* The predicate function is called one time for each element in the `input`.
*/
predicate: Task<A, boolean>;
}
interface ThrottleCommonOptions {
/**
* Max concurrency
*/
maxConcurrency: number;
/**
* Interval(in MS)
*/
interval: number;
}
interface ThrottleTaskOptions<A, B> extends ThrottleCommonOptions {
/**
* Arguments to pass to the task for each call.
*/
input: Input<A>;
/**
* The task to run for each item.
*/
task: Task<A, B>;
}
interface ThrottlePredicateOptions<A> extends ThrottleCommonOptions {
/**
* Arguments to pass to the predicate for each call.
*/
input: Input<A>;
/**
* The predicate function is called one time for each element in the `input`.
*/
predicate: Task<A, boolean>;
}
declare const loop: unique symbol;
declare abstract class SharedBase<Options> {
abstract [loop]<A>(input: Input<A>, task: Task<A, any>): Promise<void>;
/**
* Performs a specified task.
*
* @template A
* @template B
* @param {RunnableTask<A, B>} task Arguments to pass to the task for each call.
* @param {A[]} [args] The task to run for each item.
* @returns {Promise<B>}
*/
abstract run<A, B>(task: RunnableTask<A, B>, ...args: A[]): Promise<B>;
/**
* Instance Options.
*/
abstract set options(options: Options);
/**
* Instance Options.
*/
abstract get options(): Options;
/**
* Performs the specified `task` for each element in the input.
*
* @template A
* @param {Input<A>} input Arguments to pass to the task for each call.
* @param {Task<A, any>} task The task to run for each item.
* @returns {Promise<void>}
*/
forEach<A>(input: Input<A>, task: Task<A, any>): Promise<void>;
/**
* Performs the specified `task` function on each element in the `input`, and returns an array that contains the results.
*
* @template A
* @template B
* @param {Input<A>} input Arguments to pass to the task for each call.
* @param {Task<A, B>} task The task to run for each item.
* @returns {Promise<B[]>}
*/
map<A, B>(input: Input<A>, task: Task<A, B>): Promise<B[]>;
/**
* Performs the specified `task` function on each element in the `input`,
* and creates a Promise that is resolved with an array of results when all of the tasks are resolve or reject.
*
* @template A
* @template B
* @param {Input<A>} input Arguments to pass to the task for each call.
* @param {Task<A, B>} task The task to run for each item.
* @returns {Promise<PromiseSettledResult<B>[]>}
*/
mapSettled<A, B>(input: Input<A>, task: Task<A, B>): Promise<PromiseSettledResult<B>[]>;
/**
* Returns the elements that meet the condition specified in the predicate function.
*
* @template A
* @param {Input<A>} input Arguments to pass to the task for each call.
* @param {Task<A, boolean>} predicate The task to run for each item.
* @returns {Promise<void>}
*/
filter<A>(input: Input<A>, predicate: Task<A, boolean>): Promise<A[]>;
/**
* Determines whether the specified `predicate` function returns true for any element of `input`.
*
* @template A Input Type.
* @param {Input<A>} input Arguments to pass to the task for each call.
* @param {Task<A, boolean>} predicate The task to run for each item.
* @returns {Promise<boolean>}
*/
some<A>(input: Input<A>, predicate: Task<A, boolean>): Promise<boolean>;
/**
* Returns the `input` value of the first `predicate` that resolves to true, and undefined otherwise.
*
* @template A Input Type.
* @param {Input<A>} input Arguments to pass to the task for each call.
* @param {Task<A, boolean>} predicate The task to run for each item.
* @returns {Promise<A | undefined>}
*/
find<A>(input: Input<A>, predicate: Task<A, boolean>): Promise<A | undefined>;
/**
* Determines whether all the elements of `input` satisfy the specified `predicate`.
*
* @template A Input Type.
* @param {Input<A>} input Arguments to pass to the task for each call.
* @param {Task<A, boolean>} predicate The task to run for each item.
* @returns {Promise<boolean>}
*/
every<A>(input: Input<A>, predicate: Task<A, boolean>): Promise<boolean>;
/**
* This method groups the elements of the `input` according to the string values returned by a provided `task`.
*
* The returned object has separate properties for each group, containing arrays with the elements in the group.
*
* @template A Input Type.
* @param {Input<A>} input Arguments to pass to the task for each call.
* @param {Task<A, string | symbol>} task A function to execute for each element in the `input`. It should return a value that can get coerced into a property key (string or symbol) indicating the group of the current element.
* @returns {Promise<Group<A>>}
*/
group<A>(input: Input<A>, task: Task<A, string | symbol>): Promise<Group<A>>;
}
export { type BatchCommonOptions as B, type ConcurrencyCommonOptions as C, type Group as G, type Input as I, type RunnableTask as R, SharedBase as S, type Task as T, type BatchTaskOptions as a, type BatchPredicateOptions as b, type ConcurrencyTaskOptions as c, type ConcurrencyPredicateOptions as d, type ThrottleCommonOptions as e, type ThrottleTaskOptions as f, type ThrottlePredicateOptions as g, loop as l };