-
Notifications
You must be signed in to change notification settings - Fork 1
task
You can create a task and register a handler to process. You can define the behavior to process the task such as debounce, throttle and multiple hits.
import { HitTask } from 'datasense';You can create an instance by using new keyword.
// Create an instance of event controller so that you can on and fire events.
var task = new HitTask();You can set an options by setOptions method with an argument which has following properties.
-
maxCountThe maximum hits count. -
minCountThe minimum hits count. -
spanA time span in millisecond to reset the hits count. -
delayA number about the time span in millisecond to delay to process. -
mergeModeA string with following values to define the ignore behavior when a new raising is coming but there is a pending one there.-
debounceProcess the last one and ignore all the previous. -
monoProcess the first one and ignore the rest. -
none(default) Process all.
-
You can register one or more handlers.
task.pushHandler((arg, info) => {
console.info("Processed.", arg, info);
});And process like this way.
task.process();This method can pass an argument and this argument will be the first argument in the handler registered.
You can process a function in throttle mode as following way.
let task = HitTask.throttle(() => {
// Do something here.
}, 10000, true);This will prepare a task to process where you want to call. The 2nd argument is to set a time span to avoid the later processing. And the 3rd argument is used to declare this task is just for preparing so that it will not process immediately. To process it manually, just do like below.
task.process();In fact, the above debounce example is to set the options as following.
{
"span": 500,
"maxCount": 1
}Debounce is similar.
let task = HitTask.debounce(() => {
// Do something here.
}, 500, true);
// Call.
task.process();The 2nd argument is a time span to delay to process. The 3rd argument is to stop processing immediately.
In fact, the above debounce example is to set the options as following.
{
"delay": 500,
"mergeMode": "debounce"
}Sometimes you may need respond some events for mulitple hits such as double click for 2 hits in short, you can use multiple hits task.
let task = HitTask.multiHit(() => {
// Do something here.
}, 2, 2, 200, true);
// Call.
task.process();The 2nd argument is to set the minimum hits count to respond and the 3rd is the maximum. The 4th argument is a time span to reset the hit counter. The 5th argument is to stop processing immediately.
In fact, the above multiple hits example is to set the options as following.
{
"span": 200,
"minCount": 2,
"maxCount": 2
}You can create a schedule task if you want to process a handler periodically.
let scheduler = HitTask.schedule(info => {
// Do something here.
}, 6000);
// Start now.
scheduler.start();
// Pause.
scheduler.pause();
// Resume.
scheduler.resume();
// Start after the duration.
scheduler.start(true);
// Process the handler immediately without the controlling of scheduler.
scheduler.process();
// Stop.
scheduler.stop();