npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-worker-middleware

v1.0.0

Published

Organized Web Workers for Redux

Downloads

618

Readme

Redux Worker Middleware

build status test coverage npm version

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:

  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