generated from streamich/tpl-ts-lib
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 implement concurrency() class method decorator
- Loading branch information
Showing
2 changed files
with
19 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters