Code Monkey home page Code Monkey logo

redux-worker-middleware's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.