GitXplorerGitXplorer
k

redux-worker-middleware

public
169 stars
13 forks
4 issues

Commits

List of commits on branch master.
Unverified
696d404e8f2a677f62ca65429199b26974c87fae

1.0.0

kkeyz committed 8 years ago
Unverified
56fae3d2d8288425fa2789be11f25991e81fd28c

Merge pull request #6 from keyanzhang/1.0

kkeyz committed 8 years ago
Unverified
4fd25f33ce08da769f3f02da49d120f73b56cfe3

1.0.0 changelog

kkeyz committed 8 years ago
Unverified
6de38fa40c3a37ab3ab3d157a92e92030272cb3d

Let the middleware grab `dispatch` earlier

kkeyz committed 8 years ago
Unverified
da991e7c1d6abcc6562726d6b6b6d0cfdcca502a

Changed from `console.error` to (throw) real errors for incorrect configs.

kkeyz committed 8 years ago
Unverified
c838e6671c1e16406afed599cb4e2c68dcac972b

Major cleanup.

kkeyz committed 8 years ago

README

The README file for this repository.

Redux Worker Middleware

build status test coverage npm version

Redux + Web Workers = 💥 👷

npm install --save redux-worker-middleware

Intro

The goal of the middleware is to provide an unopinionated workflow that delegates expensive operations to Web Workers. Thus, please notice that this middleware doesn't wrap, transform, or shim Web Workers.

In case you need, webpack's worker-loader is an out of box solution for that.

API How it works

redux-worker-middleware exports a single (default) function createWorkerMiddleware. Here are the steps to set it up:

  1. Pass it a Web Worker instance and put the returned (curried) function in the middleware chain.

    • Notice that your worker should have the signature of Action -> Action; that is, it always takes a complete action and returns a complete action, which can be dispatched right away. It makes the API much simpler.
    • Need to partially update the payload? Sure, just let your worker handle the logic! It has to work anyway.
  2. To let the workers work, make sure that your action is FSA compliant and the action.meta.WebWorker field is truthy. Otherwise, the middleware will just pass the action along.

  3. If an action specifies that it needs to be processed by a worker, The middleware will obey the order. Then when the data comes back, it will be re-dispatched as a new action and be passed through all the middlewares (see #5).

Demo

I wrote this middleware as part of https://github.com/keyanzhang/repo.cat, where I need to parse a lot of markdown stuff to HTML at runtime. So the real demo can be found there: the Web Worker related parts live in actions/DataFetching.js, middlewares/worker.js, and workers/GFMParserWorker.js.

A minimal example can be found as below:

Web Worker: Add1Worker.js:

self.onmessage = ({ data: action }) => { // `data` should be a FSA compliant action object.
  self.postMessage({
    type: action.type,
    // Notice that we remove the `meta.WebWorker` field from the payload.
    // Since the returned data will be dispatched as a new action and be passed through all the middlewares,
    // keeping the `meta.WebWorker` field may cause an infinite loop.
    payload: {
      num: action.payload.num + 1,
    },
  });
};

ActionCreator:

export const add1Action = (n) => ({
  type: 'ADD_1',
  meta: {
    WebWorker: true, // This line specifies that the worker should show up and do the job
  },
  payload: {
    num: n,
  },
});

Then in your store configuration,

import { createStore, combineReducers, applyMiddleware } from 'redux';
import createWorkerMiddleware from 'redux-worker-middleware';

import * as reducers from '../reducers';
import {
  logger,
  thunk,
} from '../middlewares';

const Add1Worker = require('worker!../workers/Add1Worker'); // webpack's worker-loader
const add1Worker = new Add1Worker;

const workerMiddleware = createWorkerMiddleware(add1Worker);

const rootReducer = combineReducers(reducers);

const createStoreWithMiddleware = applyMiddleware(
  workerMiddleware,
  thunk,
  logger,
)(createStore);

// ... ...

That's it! Now when you fire an add1Action, the worker will show up and do the computation. The result (action) will be re-dispatched as a new action and be passed through all the middlewares.

Notes

For now, we don't really care if you actually pass it a real Worker instance; as long as it look likes a Worker and works like a Worker (i.e., has a postMessage method), it is a Worker. The reason behind is that we want to support Web Worker shims in an easy manner.

License

MIT