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

redux-worker-middleware's People

Contributors

alvarowolfx avatar jebeck avatar keyz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

redux-worker-middleware's Issues

Add support for Transferable Objects

The Web Worker API supports transferring ownership of certain types of objects. This skips the structured cloning of objects, which can be costly and block the main thread. I believe this was not used as much before Spectre. This middleware currently does not allow for this functionality.

My simple implementation lets the message-post-er add an additional meta field called transfer - an array of objects that should be transferred to the worker thread. If this field exists and is an array, it is provided as the second argument to postMessage and the transferrable object is given to the worker thread.

So for example, my action looks like this, where payload.buffer is a large ArrayBuffer to be processed in the worker thread.

return {
    payload,
    meta: { WebWorker: true, transfer: [payload.buffer] },
};

My implementation:
https://github.com/psychedelicious/redux-worker-middleware/blob/21a8eb1b342f415a554b28c0ca69ab6c91fdd2d7/src/index.js#L37

I'm not sure if my implementation is good, and I don't know how to write the tests ๐Ÿ˜…, so I am not submitting a PR at this time.

Of course when posting a message from the worker back to the main thread, you have direct control over the content of the message and manually configure the message to transfer ownership per usual.

Thanks for your time.

prevent infinite loop

Couldn't the middleware remove the meta info itself, if the posted back action is of the same type?

not passing along action to other middlewares?

Can you explain the reasoning behind not passing along the action to subsequent middlewares in the chain? I'd like to see my Web Worker actions in my redux-logger middleware, for example. I might be able to solve this by reordering my middlewares, but I'm just wondering if there's a reason not to keep passing the action along? The other thing I'll need to do eventually is update my state tree that tracks requests that are pending for surfacing to the user re: status...and I see the initial message to the worker as a "request" (for work).

Happy to open a PR if your answer is "no reason, just didn't think of it" ๐Ÿ™‚

worker error

add1worker.js:

worker.onmessage = ({ data: action }) => {
  worker.postMessage({
    type: action.type,
    payload: {
      num: action.payload.num + 1,
    }
  });
};

error:

Uncaught Error: The worker instance is expected to have a postMessage method.

@keyanzhang

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.