idle-tasks
v0.1.3
Published
The idle-tasks module is for optimization tasks which it can be running lazy. It may be useful for boost your page loading time.
Downloads
1
Maintainers
Readme
idle-tasks
The common web apps use lot's of javascript today. The javascript on your web is not only own but also third party's. So you need define priority for better performance and some less important code evaluate lazy. The idle-tasks module allow you define queue of tasks which will be evaluated after browser is idle. It use requestIdleCallback under the hood. If requestIdleCallback was not support in browser it would use setTimeout.
Installation
npm i idle-tasks --save
Usage
import { createIdleQueue, Event } from 'idle-tasks';
function createTask() {
return (deadline) => {
return new Promise((resolve) => {
setTimeout(resolve, 100 + time);
})
}
}
const idleQueue = createIdleQueue({
ensureTasks: true,
timeout: 1000
});
idleQueue.addTask(createTask());
idleQueue.addTask(createTask());
idleQueue.addTask(createTask());
idleQueue.addTask(createTask());
idleQueue.addTask(createTask());
idleQueue.on(Event.Start, () => console.log('start tasks'));
idleQueue.on(Event.Finish, ({ results }) => console.log('finish tasks', ...results));
idleQueue.on(Event.Error, ({ error }) => console.error(error));
// for running tasks immediately
// idleQueue.run();
// for scheduling tasks
idleQueue.schedule();