Invoker
Modifies method invocation behavior.
Installation
npm install method-invoker
Usage
The example bellow shows how to limit the concurrency of a function while applying a delay to its execution.
var Invoker = ; var startTime = Date; var { console;}; var invoker = ; var funcB = invoker; ;;;;; /*the output would be something like this:funcA 1005funcA 1006funcA 2006funcA 2006funcA 3007*/
This can also be used with an asynchronous function, like the example bellow:
var Invoker = ; var startTime = Date; var { console; ;}; var invoker = ; var funcB = invoker; var { console;}; ;;;;; /*the output should be something like this:funcA 5callback 2006funcA 3007callback 5009funcA 6010callback 8011funcA 9013callback 11015funcA 12016callback 14018*/
API
First, you need a new Invoker for each invocation control (the same one can be applied to multiple functions, and all of them will share behavior).
var invoker = ;
new Invoker()
This creates and retuns a new Invoker.
Static Methods
Invoker.params(...)
Returns a function decorator for filling the specified parameters. You can pass Invoker.CALLBACK as a replacement for the callback, which also indicates the function is asynchronous.
Invoker.concurrency(count)
Returns a function decorator for retricting concurrency. This only makes sense for asynchronous functions.
Invoker.delay(ms)
Returns a function decorator for delaying the execution of a function.
Invoker.interval(ms)
Returns a function decorator for imposing a delay between calls.
Invoker.gap(ms)
Returns a function decorator for imposing a gap between the end of a function execution and the next call. This only makes sense for asynchronous functions.
Invoker.every(count)
Returns a function decorator for executing a function every X call.
Invoker.after(count)
Returns a function decorator for executing a function after X calls.
Invoker.limit(count)
Returns a function decorator for executing a function at most X times.
Invoker.throttle(rate)
Returns a function decorator for executing a function respecting the rate limit.
Prototype Methods
Invoker#execution(fn)
Adds the specified function to the list of decorators to be applied.
Invoker#decorate(fn, ctx)
Decorates the specified function with all the configured methods, creating a new function that returns a Promise.
Invoker#getDecorator(ctx)
Returns a function with all the configured methods, that can be called with the function to be decorated.
Other
All the static methods have a prototype version, which calls Invoker#execution on the returned decorator.