GithubHelp home page GithubHelp logo

terkelg / workshy Goto Github PK

View Code? Open in Web Editor NEW
80.0 5.0 3.0 38 KB

A small (376B) lazy function scheduler for a butter smooth main thread.

License: MIT License

JavaScript 78.56% HTML 21.44%
scheduler task-scheduler throttle lazy queue-tasks performance

workshy's Introduction

workshy Build Status Version

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:

<script src="https://unpkg.com/workshy"></script>

Usage

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.

workshy(() => console.log('Hello World'), {force: false, priority: 2});
//=> 'Hello World'

options.force

Type: Boolean
Default: false

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

License

MIT ยฉ Terkel Gjervig

workshy's People

Contributors

0xflotus avatar terkelg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

workshy's Issues

requestIdleCallback is undefined and raises issues on Safari

Heyo,

Thanks for the cool library, really interesting ๐Ÿ‘!

In the current implementation requestIdleCallback is presumably falling back to the browser global window but it fails to do so in Safari.

const onIdle = requestIdleCallback || function (handler) {

I've experienced this issue before when doing feature detection checks and fixed it by explicitly setting it to window.requestIdleCallback.

screen shot 2019-01-21 at 22 54 33

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.