Skip to content

Commit

Permalink
feat: 🎸 implement concurrency() class method decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 11, 2024
1 parent 225ede3 commit 1e00c9e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/concurrencyDecorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {concurrency as _concurrency} from './concurrency';

/**
* A class method decorator that limits the concurrency of the method to the
* given number of parallel executions. All invocations are queued and executed
* in the order they were called.
*/
export function concurrency<This, Args extends any[], Return>(limit: number) {
return (
target: (this: This, ...args: Args) => Promise<Return>,
context?: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Promise<Return>>,
) => {
const limiter = _concurrency(limit);
return async function (this: This, ...args: Args): Promise<Return> {
return limiter(async () => await target.call(this, ...args));
};
};
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './base64';
export * from './Cache';
export * from './codeMutex';
export * from './concurrency';
export {concurrency as concurrencyDecorator} from './concurrencyDecorator';
export * from './dataUri';
export * from './Defer';
export * from './fanout';
Expand Down

0 comments on commit 1e00c9e

Please sign in to comment.