
Security News
Researcher Exposes Zero-Day Clickjacking Vulnerabilities in Major Password Managers
Hacker Demonstrates How Easy It Is To Steal Data From Popular Password Managers
redux-blabber
Advanced tools
Redux store enhancer for synchronizing states and actions across store instances
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.
npm install --save redux-blabber
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
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.
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
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)
This project is licensed under the MIT License - see the LICENSE.md file for details
FAQs
Redux store enhancer for synchronizing states and actions across store instances
The npm package redux-blabber receives a total of 6 weekly downloads. As such, redux-blabber popularity was classified as not popular.
We found that redux-blabber demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Hacker Demonstrates How Easy It Is To Steal Data From Popular Password Managers
Security News
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.