MCPcopy Index your code
hub / github.com/alewin/useWorker

github.com/alewin/useWorker @4.0.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release 4.0.1 ↗ · + Follow
28 symbols 76 edges 26 files 0 documented · 0% 9 cross-repo links updated 33d ago@koale/useworker@4.1.2 · 2025-03-09★ 3,11317 open issues

Browse by type

Functions 25 Types & classes 3
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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

  • [x] Kill Web Worker
  • [x] Reactive web worker status
  • [x] Add timeout option
  • [x] Import and use remote script inside useWorker function
  • [x] support Transferable Objects
  • [x] Testing useWorker #41
  • [x] 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)

📜 License

MIT


Netlify Status

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Function 25
Enum 2
Interface 1

Languages

TypeScript100%

Modules by API surface

packages/useWorker/src/lib/jobRunner.ts3 symbols
packages/examples/src/pages/Transferable/index.jsx3 symbols
packages/website/src/pages/index.js2 symbols
packages/useWorker/src/useWorker.ts2 symbols
packages/examples/src/pages/Sorting/index.js2 symbols
packages/examples/src/pages/ExternalScripts/index.js2 symbols
packages/examples/src/pages/Csv/index.js2 symbols
packages/useWorker/test/utils.js1 symbols
packages/useWorker/test/useWorker.test.js1 symbols
packages/useWorker/test/localDeps.test.js1 symbols
packages/useWorker/src/lib/status.ts1 symbols
packages/useWorker/src/lib/remoteDepsParser.ts1 symbols

Dependencies from manifests, versioned

@babel/plugin-proposal-optional-chaining7.9.0 · 1×
@changesets/cli2.13.1 · 1×
@docusaurus/core2.0.0-alpha.70 · 1×
@docusaurus/preset-classic2.0.0-alpha.70 · 1×
@koale/useworkerworkspace:^3.3.0 · 1×
@mdx-js/react1.5.2 · 1×
@testing-library/react-hooks3.4.2 · 1×
@types/react16.9.34 · 1×
@typescript-eslint/eslint-plugin2.27.0 · 1×
babel-eslint10.1.0 · 1×
babel-loader8.1.0 · 1×
chai4.2.0 · 1×

For agents

$ claude mcp add useWorker \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page