Skip to content
πŸ—ƒ Offload your store management to a worker easily.
Branch: master
Clone or download

Latest commit

developit Merge pull request #10 from Kanaye/fix-#8
Check if postMessage has 1 argument.
Latest commit dd83f17 Mar 8, 2019


Type Name Latest commit message Commit time
Failed to load latest commit information.
demo Add demo Jan 18, 2018
src Merge pull request #10 from Kanaye/fix-#8 Mar 8, 2019
test Fix chrome on travis Jan 18, 2018
.editorconfig Initial release! Jan 18, 2018
.gitignore Add demo Jan 18, 2018
.travis.yml Fix CI Jan 18, 2018 Docs for `freeze()` / `unfreeze()` Jan 29, 2018
package.json Add missing dev-dependency "gzip-size-cli". Jan 31, 2018

npm travis


Offload your store management to a worker.

Stockroom seamlessly runs a Unistore store (and its actions) in a Web Worker, setting up optimized bidirectional sync so you can also use & subscribe to it on the main thread.

  • Easy same API as unistore - a simple add-on
  • Opt-in centralized actions with the option of running on the main thread
  • Convenient action selector shorthand - no action creator needed for simple actions
  • Gracefully degrades - feature-detect Worker support and fall back to stockroom/inline

Table of Contents


Stockroom requires that you install unistore (300b) as a peer dependency.

npm install --save unistore stockroom


We'll have two files: index.js and worker.js. The first is what we import from our app, so it runs on the main thread - it imports our worker (using worker-loader or workerize-loader) and passes it to Stockroom to create a store instance around it.


import createStore from 'stockroom'
import StoreWorker from 'worker-loader!./worker'

let store = createStore(new StoreWorker())

let increment = store.action('increment')

// Let's run a registered "increment" action in the worker.
// This will eventually log a state update to the console - `{ count: 1 }`

The second file is our worker code, which runs in the background thread. Here we import Stockroom's worker-side "other half", stockroom/worker. This function returns a store instance just like createStore() does in Unistore, but sets things up to synchronize with the main/parent thread. It also adds a registerActions method to the store, which you can use to define globally-available actions for that store. These actions can be triggered from the main thread by invoking store.action('theActionName') and calling the function it returns.


import createStore from 'stockroom/worker'

let store = createStore({
  count: 0

store.registerActions( store => ({
  increment: ({ count }) => ({ count: count+1 })
}) )

export default store  // if you wish to use `stockroom/inline`



The main stockroom module, which runs on the main thread.


Given a Web Worker instance, sets up RPC-based synchronization with a WorkerStore running within it.


  • worker Worker An instantiated Web Worker (eg: new Worker('./store.worker.js'))


import createStore from 'stockroom'
import StoreWorker from 'worker-loader!./store.worker'
let store = createStore(new StoreWorker)

Returns Store synchronizedStore - a mock unistore store instance sitting in front of the worker store.


Used to run your whole store on the main thread. Useful non-worker environments or as a fallback.


For SSR/prerendering, pass your exported worker store through this enhancer to make an inline synchronous version that runs in the same thread.


  • workerStore WorkerStore The exported store instance that would have been invoked in a Worker


let store
if (SUPPORTS_WEB_WORKERS === false) {
	let createStore = require('stockroom/inline')
	store = createStore(require('./store.worker'))
else {
	let createStore = require('stockroom')
	let StoreWorker = require('worker-loader!./store.worker')
	store = createStore(new StoreWorker())
export default store

Returns Store inlineStore - a unistore instance with centralized actions


The other half of stockroom, which runs inside a Web Worker.


Creates a unistore instance for use in a Web Worker that synchronizes itself to the main thread.


  • initialState Object Initial state to populate (optional, default {})


import createWorkerStore from 'stockroom/worker'
let initialState = { count: 0 }
let store = createWorkerStore(initialState)
	increment(state) {
		return { count: state.count + 1 }

Returns WorkerStore workerStore (enhanced unistore store)


Queue all additional processing until unfrozen. freeze/unfreeze manages a cumulative lock: unfreeze must be called as many times as freeze was called in order to remove the lock.


Remove a freeze lock and process queued work.


MIT License Β© Jason Miller

You can’t perform that action at this time.