MCPcopy Index your code
hub / github.com/dai-shi/react-worker-components

github.com/dai-shi/react-worker-components @v0.2.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.2.0 ↗ · + Follow
51 symbols 128 edges 26 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

react-worker-components

CI npm size discord

React Worker Components simplify using Web Workers

Introduction

This is an experimental project inspired by React Server Component.

I've been developing several libraries to interact with Web Workers.

While they provide various interfaces with good abstraction, RSC style would be another approach which is useful for Web Workers.

RWC is a library to provide RSC-like interface for Web Workers. It serializes React elements keeping their referential identities as much as possible. If a React component is "registered", it will be referenced by string names, and it can be used at the both ends.

Project Status: Experimental but basic examples are working. Welcome to try realistic examples.

Install

npm install react-worker-components

Usage

TextBox.js

This is a component that can be used in the RWC tree. register is important to enable serialization.

import React, { useState } from 'react';

import { register } from 'react-worker-components';

export const TextBox = () => {
  const [text, setText] = useState('');
  return (



      <span>Text: {text}</span>
      <input value={text} onChange={(event) => setText(event.target.value)} />



  );
};

register(TextBox, 'TextBox');

Hello.worker.js

This is a component that runs only on web workers. expose is necessary to communicate with the main thread.

import React from 'react';

import { expose } from 'react-worker-components';

import { TextBox } from './TextBox';

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

const Hello = ({ count, children }) => {
  const fibNum = fib(count);
  return (





Hello from worker: {fibNum}


      <h1>Main TextBox</h1>
      {children}
      <h1>Worker TextBox</h1>
      <TextBox />



  );
};

expose(Hello);

App.js

This is the entry point component in the main thread. wrap is to communicate with the worker thread.

import React, { Suspense, useState } from 'react';

import { wrap } from 'react-worker-components';

import { TextBox } from './TextBox';

const Hello = wrap(() => new Worker(new URL('./Hello.worker', import.meta.url)));

export const App = () => {
  const [count, setCount] = useState(1);
  return (



      <span>Count: {count}</span>
      <button type="button" onClick={() => setCount(count + 1)}>+1</button>
      <button type="button" onClick={() => setCount((c) => c - 1)}>-1</button>
      <Suspense fallback="Loading...">
        <Hello count={count}>
          <TextBox />
        </Hello>
      </Suspense>



  );
};

API

expose

Expose a React function component from web workers.

Parameters

  • Component React.FC\
  • key string?

Examples

// foo.worker.js
import { expose } from 'react-worker-components';

const Foo = () => {
  return <h1>Foo</h1>;
};

expose(Foo);

register

Register a component with a string name

This allows serializing components between main and worker threads.

Parameters

  • component AnyComponent
  • name string

Examples

import { register } from 'react-worker-components';

const Counter = () => {
  const [count, setCount] = useState(0);
  return 

{count}<button onClick={() => setCount(1)}>1</button>

;
};

register(Counter, 'Counter');

wrap

Wrap an exposed component in main thread

This will connect the component in the worker thread. Requires Suspense.

It will create a dedicated worker for each createWorker function reference.

Parameters

  • createWorker any
  • key string?

Examples

import { wrap } from 'react-worker-components';

const Foo = wrap(() => new Worker(new URL('./Foo.worker', import.meta.url)));

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.

Core symbols most depended-on inside this repo

expose
called by 5
src/expose.ts
wrap
called by 5
src/wrap.ts
register
called by 5
src/register.ts
render
called by 2
src/expose.ts
serialize
called by 2
src/serializer.ts
deserialize
called by 2
src/serializer.ts
walk
called by 1
src/expose.ts
thunk
called by 1
src/expose.ts

Shape

Function 36
Method 9
Class 6

Languages

TypeScript100%

Modules by API surface

examples/04_multicomps/src/App.tsx7 symbols
examples/03_fetch/src/App.tsx6 symbols
examples/02_typescript/src/App.tsx6 symbols
src/serializer.ts5 symbols
src/register.ts4 symbols
src/expose.ts4 symbols
src/wrap.ts3 symbols
examples/04_multicomps/src/Hello.worker.tsx3 symbols
examples/03_fetch/src/PostData.tsx2 symbols
examples/03_fetch/src/Fetcher.worker.tsx2 symbols
examples/02_typescript/src/Hello.worker.tsx2 symbols
examples/04_multicomps/src/TextBox.tsx1 symbols

For agents

$ claude mcp add react-worker-components \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page