All Projects → dai-shi → React Hooks Worker

dai-shi / React Hooks Worker

Licence: mit
React custom hooks for web workers

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Hooks Worker

leek
Celery Tasks Monitoring Tool
Stars: ✭ 77 (-73.99%)
Mutual labels:  worker
Worker
The Hoa\Worker library.
Stars: ✭ 25 (-91.55%)
Mutual labels:  worker
celery.node
Celery task queue client/worker for nodejs
Stars: ✭ 164 (-44.59%)
Mutual labels:  worker
react-use-comlink
Three ways to use Comlink web workers through React Hooks (and in a typesafe manner).
Stars: ✭ 39 (-86.82%)
Mutual labels:  worker
worker
Worker for Vela (Target's official Pipeline Automation Framework)
Stars: ✭ 27 (-90.88%)
Mutual labels:  worker
workit
Extensible worker for Node.js that works with both Zeebe and Camunda BPM platforms powered by TypeScript
Stars: ✭ 51 (-82.77%)
Mutual labels:  worker
wtsqs
Simplified Node AWS SQS Worker Wrapper
Stars: ✭ 18 (-93.92%)
Mutual labels:  worker
Task Worklet
Task Worklet: explainer, polyfill and demos.
Stars: ✭ 257 (-13.18%)
Mutual labels:  worker
event-worker
A simpler way of dealing with Web Workers
Stars: ✭ 18 (-93.92%)
Mutual labels:  worker
tgrid
TypeScript Grid Computing Framework supporting RFC (Remote Function Call)
Stars: ✭ 83 (-71.96%)
Mutual labels:  worker
iworker
Promise-based wrapper for worker_threads
Stars: ✭ 18 (-93.92%)
Mutual labels:  worker
souls
SOULs 🔥 Build Serverless Apps faster like Rails. Powered by Ruby GraphQL, RBS/Steep, Active Record, RSpec, RuboCop, and Google Cloud.
Stars: ✭ 327 (+10.47%)
Mutual labels:  worker
wolfpacs
WolfPACS is an DICOM load balancer written in Erlang.
Stars: ✭ 1 (-99.66%)
Mutual labels:  worker
rsmq-promise
Promise interface for RSMQ
Stars: ✭ 28 (-90.54%)
Mutual labels:  worker
dbmq
Docker-based Message Queuing
Stars: ✭ 39 (-86.82%)
Mutual labels:  worker
parallelizer
Simplifies the parallelization of function calls.
Stars: ✭ 62 (-79.05%)
Mutual labels:  worker
msw-storybook-addon
Mock API requests in Storybook with Mock Service Worker.
Stars: ✭ 168 (-43.24%)
Mutual labels:  worker
Laravel Aws Worker
Run Laravel (or Lumen) tasks and queue listeners inside of AWS Elastic Beanstalk workers
Stars: ✭ 272 (-8.11%)
Mutual labels:  worker
mi-prometheus
Enabling reproducible Machine Learning research
Stars: ✭ 41 (-86.15%)
Mutual labels:  worker
amqp-delegate
A simple, but performant, remote worker system that uses AMQP to coordinate jobs.
Stars: ✭ 22 (-92.57%)
Mutual labels:  worker

react-hooks-worker

CI npm size discord

React custom hooks for web workers.

Introduction

Web Workers are another thread from the main thread in browsers. We can run heavy computation in a separate thread so that users don't feel slowing down.

React provides a reactive system. This library hides the async nature of Web Workers with React custom hooks. Results returned by Web Workers are stored in a React local state.

Developers can implement a worker as:

  • sync function
  • async function
  • sync generator function
  • async generator function

Install

npm install react-hooks-worker

Usage

slow_fib.worker.js:

import { exposeWorker } from 'react-hooks-worker';

const fib = i => (i <= 1 ? i : fib(i - 1) + fib(i - 2));

exposeWorker(fib);

app.js:

import React from 'react';

import { useWorker } from 'react-hooks-worker';

const createWorker = () => new Worker('./slow_fib.worker', { type: 'module' });

const CalcFib = ({ count }) => {
  const { result, error } = useWorker(createWorker, count);
  if (error) return <div>Error: {error}</div>;
  return <div>Result: {result}</div>;
};

const App = () => (
  <div>
    <CalcFib count={5} />
  </div>
);

Bundler support requirements

This library requires a bundler to recognize Web Worker properly. Not everything is tested, and we appreciate for your help. (So far, only tested with worker-plugin.)

Webpack

Parcel

Parcel allow your Web Worker script to be automatically bundled.

Rollup

API

exposeWorker

expose worker

You can expose any function that returns:

  • A value
  • A promise
  • An iterable
  • An async iterable

Parameters

  • func function (data: any): any

Examples

import { exposeWorker } from 'react-hooks-worker';

const fib = (i) => (i <= 1 ? i : fib(i - 1) + fib(i - 2));

exposeWorker(fib);

useWorker

use worker

The createWorker function should be stable to keep the worker running. If it's referentially changed, it will create a new worker and terminate the old one.

Parameters

  • createWorker function (): Worker
  • input any

Examples

import { useWorker } from 'react-hooks-worker';

const createWorker = () => new Worker('./slow_fib.worker', { type: 'module' });

const CalcFib = ({ count }) => {
  const { result, error } = useWorker(createWorker, count);
  if (error) return <div>Error: {error}</div>;
  return <div>Result: {result}</div>;
};

Examples

The examples folder contains working examples. You can run one of them with

PORT=8080 npm run examples:01_minimal

and open http://localhost:8080 in your web browser.

Blogs

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].