idley
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

Idley

Helper functions for implementing the idle-until-urgent pattern. For detailed explanation of this pattern, see this article from Philip Walton.

Installation

// with yarn
yarn add idley

// with npm
npm install idley

Usage

// ES2015+ and TS
import { computed, throttled, debounced } from 'idley'
 
// CommonJS
var idley = require('idley')

Functions

Computed

computed(fn: function): function

Creates a function that returns the result of executing the passed fn when the browser is idle. If the result is requested and it has not been computed yet, the function will be executed immediately.

import { computed } from 'idley'
 
const heavyTask = () => 5
 
const foo = computed(heavyTask)
 
console.log(foo()) // 5

Throttled

throttled(fn: function): function

Creates a throttled function that only invokes the passed fn at most once when the browser is idle.

import { throttled } from 'idley'
 
const heavyTask = (n) => console.log(n)
 
const foo = throttled(heavyTask)
 
foo(1)
foo(2)
 
// 2

Debounced

debounced(fn: function): function

Creates a debounced function that delays invoking the passed fn until the browser is idle.

import { debounced } from 'idley'
 
const heavyTask = (n) => console.log(n)
 
const foo = debounced(heavyTask)
 
foo(1)
foo(2)
 
// 1
// 2

This library relies on requestIdleCallback. It will degrade to requestAnimationFrame, or setTimeout in this order

Published under MIT Licence

(c) Yosbel Marin 2018

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i idley

      Weekly Downloads

      8

      Version

      0.2.2

      License

      MIT

      Unpacked Size

      15.5 kB

      Total Files

      26

      Last publish

      Collaborators

      • yosbelms