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

@getstation/redux-broadcast-actions

v1.1.0

Published

A tiny middleware for redux to share actions through BroadcastChannel API

Downloads

55

Readme

redux-broadcast-actions

A tiny middleware to broadcast your redux actions using the Broadcast Channel API. It can sync actions between tabs, and even between all processes of a Browser Extension (background, workers, content scripts, popup, custom pages). Then you just need to plug all your pure reducers onto all your processes, and you have a state that is synced as simply as possible.

How to install

Install with npm or yarn

npm install --save @getstation/redux-broadcast-actions
# or
yarn add @getstation/redux-broadcast-actions

How to use

In all the processes where you need the actions to be dispatched and received, instanciate this middlware:

import { createStore, applyMiddleware } from 'redux';
import { createBroadcastActionsMiddleware } from '@getstation/redux-broadcast-actions';

const store = createStore(rootReducer, {}, applyMiddleware(
  // broadcast actions to other processes
  createBroadcastActionsMiddleware({
    // name given to BroadcastChannel. Can also be a BroadcastChannel instance
    channel: 'redux_broadcast_actions',
  })
));

Then whenever you execute store.dispatch(...) in any process, all others are receiving the exact same action.

Share initial state

If one of your process acts as a source of truth (server) and other processes (clients) need to ask for its current state to be initialized:

Server
import { createStore, applyMiddleware } from 'redux';
import {
  createBroadcastActionsMiddleware,
  createSendInitialStateMiddleware
} from '@getstation/redux-broadcast-actions';

const store = createStore(rootReducer, {}, applyMiddleware(
  // this middleware listens for clients asking for initial state
  createSendInitialStateMiddleware(),
  createBroadcastActionsMiddleware(),
));
Client
import { createStore, applyMiddleware, combineReducers } from 'redux';
import {
  createBroadcastActionsMiddleware,
  createGetInitialStateMiddleware,
  initialStateReducer
} from '@getstation/redux-broadcast-actions';

// add initialStateReducer to the list of reducers
const store = createStore(combineReducers(initialStateReducer, rootReducer), {}, applyMiddleware(
  // ask server for initial state
  // ⚠️ should be the first middleware
  createGetInitialStateMiddleware(),
  createBroadcastActionsMiddleware(),
));

I do not want to broadcast one or more actions

You can do that. First, by default some events of some libs are not broadcasted (redux-form, redux-persist, and some others, check shouldForward function for details). If you do not want to broadcast one of your action, you can leverage the meta property of your action like this:

dispatch({
  type: 'myAction',
  meta: {
    // this tells the middleware to not broadcast this action
    local: true,
  }
})