Redux Bundler Web Worker Utilities

0.0.3 · abandoned · verified Tue Apr 21

redux-bundler-worker provides utilities for offloading an entire redux-bundler application into a Web Worker, allowing the main thread to focus solely on rendering. It achieves this by offering a `getStoreProxy` function for the main thread, which mimics the redux-bundler store API, and a `setUpWorker` function for the worker thread to initialize the actual redux-bundler store. This approach aims to demonstrate a feature-complete mechanism for isolating Redux logic. The current stable version, `0.0.3`, is explicitly stated as being "largely for demonstration purposes" and not shipped in any production applications by its author. As such, it does not have a clear release cadence and should be considered experimental. Its key differentiator is the complete isolation of Redux state management and logic within a worker, reducing main thread overhead.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up `redux-bundler-worker` by initializing a Web Worker in the main thread and establishing a proxied Redux store for communication. It shows how to use `getStoreProxy` in the main thread with a UI library like Preact and `setUpWorker` inside the Web Worker to host the actual `redux-bundler` instance, including a basic bundle with actions and selectors.

/* --- main.js (Main Thread) --- */
import { render, h } from 'preact';
import { Provider } from 'redux-bundler-preact';
import { getStoreProxy } from 'redux-bundler-worker';

// A dummy RootComponent for demonstration
const RootComponent = ({ children }) => h('div', null, h('h1', null, 'App Running in Worker'), children);

// Create the Web Worker instance, pointing to your bundled worker script
// Ensure '/build/worker.js' is correctly served by your web server/bundler
const worker = new Worker('/build/worker.js', { type: 'module' });

// Get a store proxy that mimics the redux-bundler store API
// All actions dispatched and selectors called on this proxy will be marshalled to the worker
const storeProxy = getStoreProxy(worker);

// Render your Preact app, passing the store proxy to the Provider
render(
  h(Provider, { store: storeProxy }, h(RootComponent)),
  document.getElementById('app-root')
);

console.log('Main thread initialized with worker-proxied Redux store.');

/* --- worker.js (Web Worker Thread) --- */
import { composeBundles } from 'redux-bundler';
import { setUpWorker } from 'redux-bundler-worker';

// Define a simple Redux Bundler bundle inside the worker
const appBundle = {
  name: 'app',
  reducer: (state = { count: 0 }, action) => {
    if (action.type === 'INCREMENT') {
      return { ...state, count: state.count + 1 };
    }
    return state;
  },
  doIncrement: () => ({ dispatch }) => {
    dispatch({ type: 'INCREMENT' });
  },
  selectCount: (state) => state.app.count,
};

// Compose the bundles to create the main Redux Bundler store function
const getWorkerStore = (initialData = {}) => composeBundles(appBundle)(initialData);

// Set up the worker, passing the function that returns the composed store
setUpWorker(getWorkerStore);

console.log('Web Worker initialized with Redux Bundler store.');

// Example of interacting from the main thread after setup (e.g., in browser console)
// window.store = storeProxy; // expose for debugging
// storeProxy.doIncrement();
// storeProxy.selectCount();

view raw JSON →