Socket
Book a DemoInstallSign in
Socket

redux-blabber

Package Overview
Dependencies
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redux-blabber

Redux store enhancer for synchronizing states and actions across store instances

0.1.2
latest
Source
npmnpm
Version published
Weekly downloads
7
Maintainers
2
Weekly downloads
 
Created
Source

redux-blabber

Redux store enhancer for synchronizing states and actions across store instances.

Blabber is designed to make stores talk to each other. The idea is to synchronize the full state once and then sync individual actions afterwards

In other words: It makes the redux store blabber on about what is happening.

It uses Observable and Observers to communicate and therefore supports synchronization through anything that implements their interfaces.

Getting Started

Installing

npm install --save redux-blabber

Usage

A single store should be chosen as master. This is the store, that will hydrate other connected stores when the hydrate-action is dispatched. Below is

import { masterReducer, slaveReducer } from './myReducers';
import { createStore }                 from 'redux';
import blabberEnhancer, { hydrate }    from 'redux-blabber';

// A quick and dirty communication stream using Rx.Subject
import { Subject } from 'rxjs/Subject';
const stream1 = new Subject()
const stream2 = new Subject()

// MASTER SETUP

const masterEnhancer = blabberEnhancer({
  // Ingoing actions using the Rx.Observable interface
  receive$:     stream1,     
  // Outgoing actions using the Rx.Observer interface
  transmit$:    stream2,      
  // Filter which actions should be distributed (let's through all actions per default)
  predicate:    (action) => action.type === 'MY_ACTION',
  // Map out which parts of state should hydrate on full syncs (full state sync is default)
  hydrationMap: { slice1: 'fee', slice2: 'fi.fo' }, 
  // Note whether this store instance should send or receive full state on hydration (master sends)
  isMaster:     true,        
  // Create custom ID for the blabberInstance (optional)
  createUUID:   () => 'master',
})

const masterStore = createStore(masterReducer, masterEnhancer)

// SLAVE SETUP

// Add other middleware 
const slaveEnhancer = blabberEnhancer({
  receive$:     stream2, //inverted order of streams
  transmit$:    stream1,      
  predicate:    (action) => action.type === 'MY_ACTION',
  hydrationMap: { slice1: 'fee', slice2: 'fo' }, 
  isMaster:     false,        
})

const slaveStore = createStore(slaveReducer, slaveEnhancer)

// Initial full state sync
slaveStore.dispatch(hydrate())

// Action sync
masterStore.dispatch({type: 'MY_ACTION'})
slaveStore.dispatch({type: 'MY_ACTION'})
// 'MY_ACTION' is triggered two times on both stores

Full state syncs

A single store should be chosen as master, that will hydrate other connected stores with its state on demand. A state synchronization is triggered whenever either a master or slave dispatch the hydrate() action (this should always be done when a new blabbering store is connected)

The above hydrationMap configuration gives the below redux states:

const masterState = {
  fee:  'synced1',
  fi: {
    fo: 'synced2'
  },
  fum:  'local'
}

const slaveState = {
  fee: 'synced1',
  fo:  'synced2',
  fum: 'local'
}

where only fields 'fee' and 'fo' will be syncronized as the hydrate() action is dispatched on the store. Note that the supplied hydration maps must contain the same keys (otherwise, hydration for the field will be discarded), but that their state paths are flexible as in the 'slice2' example using the lodash set path syntax. An empty string as value, will sync the whole store.

Action syncs

All actions that pass the predicate will be synced. In the example above, only the action of type 'MY_ACTION' is syncronized. If no predicate is supplied, all actions are synchronized

Interoperability with other middleware

The blabberEnhancer can be used with other middlewares and store enhancers via composition

import { createStore, compose } from 'redux';

const myEnhancer = compose(
  applyMiddleware(thunk, logger),
  myOtherStoreEnhancer,
  blabberEnhancer(blabberOptions),
)

const myStore = createStore(myReducer, myEnhancer)

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Keywords

Redux

FAQs

Package last updated on 05 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.