• Stars
    star
    2,768
  • Rank 16,455 (Top 0.4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⚛️ useWorker() - A React Hook for Blocking-Free Background Tasks


useWorker

Use web workers with react hook
https://useworker.js.org/
Tweet

Bytes Newsletter

GitHub size GitHub TypeScript Support


🎨 Features

  • Run expensive function without blocking UI (Show live gif)
  • Supports Promises pattern instead of event-messages
  • Size: less than 3KB!
  • Clear API using hook
  • Typescript support
  • Garbage collector web worker instance
  • Remote dependencies option
  • timeout option

💾 Install

  • @latest
npm install --save @koale/useworker

🔨 Import

import { useWorker, WORKER_STATUS } from "@koale/useworker";

📙 Documents


🍞 Demo


Web Workers

Before you start using this hook, I suggest you to read the Web Worker documentation.


🐾 Usage

import React from "react";
import { useWorker } from "@koale/useworker";

const numbers = [...Array(5000000)].map(e => ~~(Math.random() * 1000000));
const sortNumbers = nums => nums.sort();

const Example = () => {
  const [sortWorker] = useWorker(sortNumbers);

  const runSort = async () => {
    const result = await sortWorker(numbers); // non-blocking UI
    console.log(result);
  };

  return (
    <button type="button" onClick={runSort}>
      Run Sort
    </button>
  );
};

🖼 Live Demo

useworker demo


🐾 Examples

Edit white-glitter-icji4

More examples: https://github.com/alewin/useWorker/tree/develop/example


🔧 Roadmap

  • Kill Web Worker
  • Reactive web worker status
  • Add timeout option
  • Import and use remote script inside useWorker function
  • support Transferable Objects
  • Testing useWorker #41
  • Import and use local script inside useWorker function #37
  • useWorkers Hook #38
  • useWorkerFile Hook #93

🤔 Motivation and Limitations

Most react projects are initialized through Create React App. CRA unfortunately does not offer support for webworkers, unless you eject and change the webpack configuration manually.

This library allows you to use web workers without having to change the CRA configuration, which is why there are often limitations or particular workarounds.

If you are interested in changing the webpack configuration to manually manage your workers, see: worker-loader


Known issues

There's a known issue related to transpiling tools such as Babel causing Not refereced errors.

Since the approach of this library is moving the entire function passed to the Hook to a worker, if the function gets transpiled, variable definitions used by the transpiling tool may get out of scope when the function gets moved to the worker, causing unexpected reference errors.

If you're experimenting this type of issue, one workaround is wrapping your function declaration inside a function object as a string.

const sum = new Function(`a`, `b`, `return a + b`)

🌏 Contribute? Bug? New Feature?

The library is experimental so if you find a bug or would like to request a new feature, open an issue


💡 Similar Projects


💻 Mantainers

💻 Contributors

  • Thanks to:
  • @zant (test, CI, RFC, bugfixes, localdependencies feature, ...)
  • @101arrowz ( isoworker packages proposal )
  • @z4o4z (Typescript implementation, Discussion of RFC)
  • @IljaDaderko (Typescript support, Discussion of RFC)
  • @ophirg (Typescript support)
  • @Pigotz (Discussion of RFC)
  • @gubo97000 (Fix #108)

How to contribute?

Read CONTRIBUTE.md


📜 License

MIT


Netlify Status

More Repositories

1

CliChords

[CliChords] Get ultimate-guitar.com guitar tabs and chords in your terminal - command line cli
JavaScript
20
star
2

moneytracking

Money tracking - Android App for planning, tracking your spending, monitoring your credit and budget - UNIBO 2016/2017 - Alessio Koci
Java
10
star
3

google-ux-playbooks

Google UX playbooks (Retail, Finance, Automobile, Travel, Real Estate, Healthcare, Lead Gen, News and content)
8
star
4

react-tips

📚 A list of tips, patterns and performance notes about react [COMING SOON 2020]
6
star
5

watermark-images

Python Tool - add watermarks logo or text to single or multiple images/photos via command line cli
Python
5
star
6

getmp3-angularjs

Angularjs vk music player streaming download and playlist
JavaScript
4
star
7

c-code-snippets

C code snippets and system call examples, UniBo 2016 Sistemi Operativi
C
2
star
8

Unibo2016-os-exams

Operating System OS Exams C and python, UniBo 2016 Esami Sistemi Operativi
C
2
star
9

ToastyPlugin

Cordova plugin for toast message [Only Android]
Java
1
star
10

jquery-pin-annotation

jquery pin annotation plugin [ under development ]
1
star
11

EmotionDetection-MicrosoftCognitive

Emotion detection using MicrosoftCognitive
C#
1
star
12

text-to-icon

[Python script] Convert text and word to icon using natural language library ( nltk ) and thenounproject online repository
Python
1
star
13

useworker-local-deps-demo

JavaScript
1
star
14

EmotionDetection

[Intership Project] Upload video and recognize face emotion
JavaScript
1
star
15

Tesi-Triennale

Tesi Informatica Triennale Bologna - Utilizzo di Kotlin per sviluppo di applicazioni mobili
TeX
1
star
16

lru-replacement-algorithm

python script to simulate lru replacement algorithm, probably with bug.. but seems to be working
Python
1
star