npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@n0n3br/use-webworker

v0.0.1

Published

`useWebworker` is a React hook that simplifies the integration of Web Workers into your React applications. It allows you to offload computationally intensive tasks to a separate thread, preventing the main UI thread from becoming unresponsive.

Downloads

1

Readme

useWebworker Hook

useWebworker is a React hook that simplifies the integration of Web Workers into your React applications. It allows you to offload computationally intensive tasks to a separate thread, preventing the main UI thread from becoming unresponsive.

What are Web Workers?

Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface. Once created, a worker can send messages to the JavaScript code that created it by posting messages to an event handler specified by that code (and vice versa).

Web Workers are particularly useful for:

  • Performing CPU-intensive tasks: Such as complex calculations, data processing, or image manipulation without freezing the main UI thread.
  • Keeping the UI responsive: By offloading long-running tasks, the main thread remains free to handle user interactions, ensuring a smooth user experience.
  • Background data fetching and synchronization: Workers can handle network requests and data synchronization in the background.

Features

  • Easy to use: Simple API for creating and interacting with Web Workers.
  • Manages worker lifecycle: Handles worker creation, termination, and message passing.
  • TypeScript support: Fully typed for a better development experience.

Installation

npm install @n0n3br/use-webworker
# or
yarn add @n0n3br/use-webworker

API

function useWebworker<TData = any, TMessage = any>(
  workerFactory: () => Worker,
  onMessage?: (event: MessageEvent<TData>) => void,
  onError?: (event: ErrorEvent) => void
): {
  postMessage: (message: TMessage) => void;
  worker: Worker | null;
  isWorkerRunning: boolean;
  terminateWorker: () => void;
  error: ErrorEvent | null;
};

Parameters

  • workerFactory: A function that returns a new Worker instance, or a function whose body will be used as the Web Worker's code. If a function is provided to be the worker's code, it will be stringified and a new Worker will be created using a Blob URL. This function will be called to create the worker.
  • onMessage (optional): A callback function that is invoked when the worker sends a message back to the main thread. It receives the MessageEvent as an argument.
  • onError (optional): A callback function that is invoked when an error occurs in the worker. It receives the ErrorEvent as an argument.

Return Value

An object containing:

  • postMessage: A function to send messages to the Web Worker. It takes the message as an argument.
  • worker: The Worker instance. It will be null until the worker is initialized.
  • isWorkerRunning: A boolean indicating whether the worker is currently running.
  • terminateWorker: A function to terminate the Web Worker.
  • error: The last error event received from the worker, or null if no error has occurred.

Usage Example

First, create your worker file (e.g., my.worker.js):

// my.worker.js
self.onmessage = function (event) {
  const data = event.data;
  // Perform some heavy computation
  const result = data * 2;
  self.postMessage(result);
};

Then, use the useWebworker hook in your React component:

import React, { useState, useCallback } from "react";
import { useWebworker } from "@n0n3br/use-webworker";

const MyComponent: React.FC = () => {
  const [result, setResult] = useState<number | null>(null);

  const handleMessage = useCallback((event: MessageEvent<number>) => {
    setResult(event.data);
  }, []);

  const handleError = useCallback((event: ErrorEvent) => {
    console.error("Worker error:", event.message);
  }, []);

  const workerFactory = () =>
    new Worker(new URL("./my.worker.js", import.meta.url));

  const { postMessage, isWorkerRunning, terminateWorker, error } = useWebworker<
    number,
    number
  >(workerFactory, handleMessage, handleError);

  const handleClick = () => {
    if (isWorkerRunning) {
      postMessage(5); // Send a message to the worker
    }
  };

  return (
    <div>
      <h1>useWebworker Example</h1>
      <button onClick={handleClick} disabled={!isWorkerRunning}>
        Calculate (5 * 2)
      </button>
      {result !== null && <p>Result from worker: {result}</p>}
      {error && <p style={{ color: "red" }}>Error: {error.message}</p>}
      {isWorkerRunning && (
        <button onClick={terminateWorker} style={{ marginLeft: "10px" }}>
          Terminate Worker
        </button>
      )}
    </div>
  );
};

export default MyComponent;

Using a function as the worker source

You can also provide a function directly to useWebworker. The body of this function will be executed in the Web Worker.

import React, { useState, useCallback } from "react";
import { useWebworker } from "@n0n3br/use-webworker";

const MyComponentWithFunctionWorker: React.FC = () => {
  const [result, setResult] = useState<number | null>(null);

  const handleMessage = useCallback((event: MessageEvent<number>) => {
    setResult(event.data);
  }, []);

  const handleError = useCallback((event: ErrorEvent) => {
    console.error("Worker error:", event.message);
  }, []);

  // Define the worker logic as a function
  const workerFunction = () => {
    self.onmessage = (event: MessageEvent<number>) => {
      const data = event.data;
      // Perform some heavy computation
      const result = data * 3; // Example: multiply by 3
      self.postMessage(result);
    };
  };

  const { postMessage, isWorkerRunning, terminateWorker, error } = useWebworker<
    number,
    number
  >(workerFunction, handleMessage, handleError);

  const handleClick = () => {
    if (isWorkerRunning) {
      postMessage(10); // Send a message to the worker (10 * 3)
    }
  };

  return (
    <div>
      <h1>useWebworker Example (Function as Worker)</h1>
      <button onClick={handleClick} disabled={!isWorkerRunning}>
        Calculate (10 * 3)
      </button>
      {result !== null && <p>Result from worker: {result}</p>}
      {error && <p style={{ color: "red" }}>Error: {error.message}</p>}
      {isWorkerRunning && (
        <button onClick={terminateWorker} style={{ marginLeft: "10px" }}>
          Terminate Worker
        </button>
      )}
    </div>
  );
};

export default MyComponentWithFunctionWorker;

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue if you find any bugs or have suggestions for improvements.

Development Setup

  1. Clone the repository.
  2. Install dependencies: npm install or yarn install.
  3. Run tests: npm test or yarn test.
  4. Build the library: npm run build or yarn build.

Reporting Issues

If you encounter any issues, please report them on the GitHub Issues page. Provide as much detail as possible, including steps to reproduce the issue.

License

This project is licensed under the MIT License. See the LICENSE file for details.