mcall
v0.1.1
Published
A lightweight function call management tool for debounce, throttle, and custom call control of function execution.
Downloads
3
Readme
mcall
A lightweight function call management tool for debounce, throttle, and custom call control of function execution.
Installation
npm install mcall Basic Debounce (Default Mode)
import Mcall from 'mcall';
// Create a default debounce task
const task = new Mcall();
// Multiple calls, only the last one will be executed
task(() => {
console.log('Execute task 1');
});
task(() => {
console.log('Execute task 2');
}); Function Call Control Description
Executes the last passed function or concurrent.| Type | Description |
|-----------|-------------------------------------|
| delay | Debounce by time, default 300ms delay |
| num | Debounce by count, default executes after 1000ms |
| batch | Concurrent debouncing with limited parallelism |
| gap | Throttle, default executes every 200ms |
| wait | Custom, executes when .exec() is called |
Usage Examples
Custom Debounce Delay
// Create a debounce task with a 100ms delay
const task = new Mcall({
delay: 100
}); Count-Based Control
// Create a count-controlled task, executes after accumulating 2 calls
const task = Mcall({
num: 2
});
task(() => console.log('First call'));
task(() => console.log('Second call'));
task(() => console.log('Third call'));
// Manually trigger execution
task.exec(); Concurrent limitation
// Create a parallel queue to maintain a fixed number of concurrent tasks.
const task = Mcall({
batch: 2
});
task(() => new Promise(resolve=>setTimeout(() => resolve(console.log('Task 1')), 1000)));
task(() => new Promise(resolve=>setTimeout(() => resolve(console.log('Task 2')), 1000)));
task(() => new Promise(resolve=>setTimeout(() => resolve(console.log('Task 3')), 1000)));
Throttle Mode
// Create a throttle task with a 300ms interval
const task = new Mcall({
gap: 300
});
// Frequent calls, but only executes once every 300ms
task(() => {
console.log('Throttle execution 1');
});
task(() => {
console.log('Throttle execution 2');
}); Wait Mode
// Create a wait-mode task
const task = new Mcall({
type: 'wait'
});
// Add multiple tasks, but they won't execute immediately
task(() => console.log('Task 1'));
task(() => console.log('Task 2'));
// Manually trigger execution of the last task function
task.exec(); API
Instance Methods
import Mcall from 'mcall';
const task = new Mcall(opt: McallOpt); Options Explanation
type McallOpt = {
type?: 'delay' | 'num' | 'gap' | 'wait' | 'batch';
delay?: number; // Delay time (ms)
batch?: number; // Number of concurrent tasks
num?: number; // Accumulated count
gap?: number; // Throttle interval (ms)
onExec?: Function; // Execution event function callback
onRun?: Function; // Add execution event function callback
onEnd?: Function; // concurrent tasks end event function callback
} task(fn: Function): Add an execution function (same astask.run())task.run(fn: Function): Add an execution functiontask.exec(): Manually trigger executiontask.clear(): Clear the tasktask.close(): Destroy the instancetask.on(eventName: string, callback: Function): Event listener (eventName: 'run' | 'exec')
Notes
- Default mode is debounce (300ms delay)
- In throttle mode (
'gap'), the first call executes immediately - In
'wait'mode, manualexec()is required to trigger execution - In count-based (
'num') debounce mode, it executes by default after 1000ms. Setdelayto 0 orInfinityto disable auto-execution, requiring manualexec() - Use
close()to destroy the instance, cancel events, and timers
License
MIT
