Skip to content

The idle-tasks module is for optimization tasks which it can be running lazy. It may be useful for boost your page loading time.

License

Notifications You must be signed in to change notification settings

mjancarik/idle-tasks

Repository files navigation

idle-tasks

Version Build Status code style: prettier Coverage Status npm bundle size Snyk Vulnerabilities for npm package dependencies Status

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();

About

The idle-tasks module is for optimization tasks which it can be running lazy. It may be useful for boost your page loading time.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published