redux-web-worker

A redux implementation with web workers

Usage no npm install needed!

<script type="module">
  import reduxWebWorker from 'https://cdn.skypack.dev/redux-web-worker';
</script>

README

redux-worker CircleCI

Redux implementation in a web worker. The entire state is kept in a separate thread. (this also gives the added benefit of immutable objects)

import { createStore } from 'redux-worker/core';
// Or if using bundle.
// var createStore = Rw.createStore;

// the reduces is run in a new thread
var store = createStore((state, action) => {
  switch(action.type) {
    case 'INCREMENT':
      return state + 1;
    case 'DECREMENT':
      return state - 1;
    default:
      return state;
  }
}, 0);

// get state has to be async since the state is managed is a separate thread
store.getState(state => {
  console.log(state) // 0
});

// subscribe to the store for changes.
// the web worker acts as the dispatcher with onmessage and postMessage
store.subscribe(state => {
  console.log(state);
});

// standard redux style dispatch
store.dispatch({ type: 'INCREMENT' });