Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/terkelg/workshy
A small (376B) lazy function scheduler for a butter smooth main thread.
https://github.com/terkelg/workshy
lazy performance queue-tasks scheduler task-scheduler throttle
Last synced: 3 months ago
JSON representation
A small (376B) lazy function scheduler for a butter smooth main thread.
- Host: GitHub
- URL: https://github.com/terkelg/workshy
- Owner: terkelg
- License: mit
- Created: 2018-12-30T10:11:11.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-03-15T15:48:56.000Z (over 4 years ago)
- Last Synced: 2024-07-13T07:24:03.164Z (4 months ago)
- Topics: lazy, performance, queue-tasks, scheduler, task-scheduler, throttle
- Language: JavaScript
- Homepage:
- Size: 37.1 KB
- Stars: 80
- Watchers: 5
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# workshy [![Build Status](https://travis-ci.org/terkelg/workshy.svg?branch=master)](https://travis-ci.org/terkelg/workshy) [![Version](https://badgen.now.sh/npm/v/workshy)](https://npmjs.com/package/workshy)
> A small (376B) lazy function scheduler for a butter smooth main thread
Workshy is a `throttle` utility that **rate limit**, **queue**, and **distribute** function executions over time to prevent the main thread from becoming unresponsive.
Unlike a standard throttle function, and to ensure non-blocking rendering and responsive UIs, `workshy` break up functions into smaller chunks executed over time if necessary.
This module is available in three formats:
* **ES Module**: `dist/workshy.mjs`
* **CommonJS**: `dist/workshy.js`
* **UMD**: `dist/workshy.min.js`## Install
```
$ npm install --save workshy
```The script can also be directly included from [unpkg.com](https://unpkg.com/workshy):
```html```
## Usage
```js
import workshy from 'workshy';// dummy function doing heavy work
const greet = () => 'hello world';// queue and call function
workshy(greet)();
// => 'hello world'// tasks are only called once, but
// multiple calls increases priority
const a = workshy(x => console.log(`A: ${x}`));
const b = workshy(x => console.log(`B: ${x}`));
b(1);
a(1);
a(2);
// => A: 2
// => B: 1// manually define priority
const func = workshy(greet, {priority: 2});// force it to be called immediately
const func = workshy(greet, {force: true});// workshy distribute the work over time to
// make sure the main thread runs butter smooth
for (let i = 0; i < 5000; i++) {
workshy(greet)(); // => this won't block UI
}```
## API
### workshy(task, [options])
Returns: `function`#### task
Type: `function`Accepts any function a returns a `function` (a function that wraps your original function). Call returned function to queue task.
The returned `function` will execute your function with the latest arguments provided to it as soon as possible based on queue length and prioroty.
> **Important:** Task are only called _once_.
Calling the same task multiple times increases its priority.#### options.priority
Type: `Number`
Default: `0`Tasks are sorted by priority. Functions with high priority are called first.
> **Important:** Priority also increase if a task is called multiple times.
```js
workshy(() => console.log('Hello World'), {force: false, priority: 2});
//=> 'Hello World'
```#### options.force
Type: `Boolean`
Default: `false````js
workshy(() => console.log('Hello World'), {force: false, priority: 2});
//=> 'Hello World'
```## Inspiration
This is inspired by the talk [The Virtue of Laziness: Leveraging Incrementality for Faster Web UI](https://youtu.be/ypPRdtjGooc?t=510)
## License
MIT © [Terkel Gjervig](https://terkel.com)