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

react-redux-worker

v0.1.9

Published

Run a Redux store in a Web Worker

Downloads

21

Readme

react-redux-worker

Run a Redux store in a web worker.

Why?

If you're doing any sort of computationally expensive work in your Redux reducers or middleware, it can prevent your UI from responding while it's thinking—making your application feel slow and unresponsive.

In theory, web workers should be the perfect solution: You can do your heavy lifting in a worker thread without interfering with your main UI thread. But the message-based web worker API puts us on unfamiliar terrain.

This library is intended to make the developer experience of using a worker-based Redux store as similar as possible to an ordinary Redux setup.

How it works

This library provides you with a proxy Redux store. To your application, the proxy looks just like the real thing: You communicate with it synchronously using useDispatch and useSelector hooks just like the ones that the official react-redux bindings provide.

diagram

The proxy then handles messaging back and forth with the store in the worker using the Comlink library, built by the Google Chrome team.

Running the demo

yarn
yarn start

Then open http://localhost:1234 in a browser. You should see something like this:

demo

Usage

Add the dependency

yarn add react-redux-worker

Put your store in a worker, and create a proxy

In a stand-alone file called worker.ts or store.worker.ts, import your reducer (and middlewares, if applicable) and build your store the way you always have. Then wrap it in a proxy store, and expose that as a worker messaging endpoint:

// worker.ts
import { createStore } from 'redux'
import { reducer } from './reducer'
import { expose, createProxyStore } from 'react-redux-worker'

const store = createStore(reducer) // if you have initial state and/or middleware you can add them here as well
const proxyStore = createProxyStore(store)
expose(proxyStore, self)

Add a context provider for the proxy store

At the root of your app, replace your standard react-redux Provider with one that gives access to the proxy store.

import { getProvider } from 'react-redux-worker'

const worker = new Worker('./redux/worker.ts')
const ProxyProvider = getProvider(worker)

ReactDOM.render(
  <ProxyProvider>
    <App />
  </ProxyProvider>,
  document.querySelector('.root')
)

Use the proxy useDispatch and useSelector hooks in your components

import * as React from 'react'
import { useDispatch, useSelector } from 'react-redux-worker'
import { actions } from './redux/actions'

export function WithWorker() {
  const state = useSelector((s => s)
  const dispatch = useDispatch()

  dispatch(actions.setBusy(true))
  dispatch(actions.doSomeHeavyLifting())
  dispatch(actions.setBusy(false))

  return (<div>
    {state.busy ? (
      <span>Thinking...</span>
    ) : (
      <span>Result: {state.result}</span>
    )}
  </div>)
}

Prior art