# redux-blabber

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

Latest version **0.1.2** (published 2017-12-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-blabber
pnpm add redux-blabber
yarn add redux-blabber
bun add redux-blabber
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2017-12-05 |
| First published | 2017-12-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | pehnen, lukashedegaard |
| Keywords | Redux, Action, Store, Sync, Synchronization, Middleware, Store Enhancer, Rx, RxJS, Observables, Observable, ES6, ES2015, Typescript |

## Links

- npm: https://www.npmjs.com/package/redux-blabber
- Repository: https://github.com/garblovians/redux-blabber
- Homepage: https://github.com/garblovians/redux-blabber#readme
- Issues: https://github.com/garblovians/redux-blabber/issues
- npm.io page: https://npm.io/package/redux-blabber

## Dependencies (2)

- [yeast](https://npm.io/package/yeast.md) ^0.1.2
- [lodash](https://npm.io/package/lodash.md) ^4.17.4

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 0.1.2 (latest) — 2017-12-05
- 0.1.1 — 2017-12-04

## README

# redux-blabber

Redux [store enhancer](https://github.com/reactjs/redux/blob/master/docs/Glossary.md#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](http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html) and [Observers](http://reactivex.io/rxjs/class/es6/MiscJSDoc.js~ObserverDoc.html) 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

```js
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:
```js
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](https://lodash.com/docs/4.17.4#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
```js
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](LICENSE.md) file for details

---
_Source: https://npm.io/package/redux-blabber · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
