Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yosbelms/idley
Helper functions for implementing the idle-until-urgent pattern
https://github.com/yosbelms/idley
Last synced: 16 days ago
JSON representation
Helper functions for implementing the idle-until-urgent pattern
- Host: GitHub
- URL: https://github.com/yosbelms/idley
- Owner: yosbelms
- Created: 2018-09-21T04:47:56.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-09-22T23:18:03.000Z (about 6 years ago)
- Last Synced: 2024-09-27T03:44:09.219Z (about 2 months ago)
- Language: TypeScript
- Size: 6.84 KB
- Stars: 37
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Idley
Helper functions for implementing the idle-until-urgent pattern. For detailed explanation of this pattern, see [this](https://philipwalton.com/articles/idle-until-urgent/) article from [Philip Walton](https://github.com/philipwalton).
## Installation
```
// with yarn
yarn add idley// with npm
npm install idley
```## Usage
```ts
// 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.
```ts
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.
```ts
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.
```ts
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