redux-worker-middleware
v1.0.0
Published
Organized Web Workers for Redux
Downloads
1,047
Maintainers
Readme
Redux Worker Middleware
Redux + Web Workers = :boom: :construction_worker:
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:
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.
- Notice that your worker should have the signature of
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.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