akingo-redux-sync
v0.12.7
Published
A redux store enhancer which allows to mirror one store as a part of another
Downloads
10
Maintainers
Readme
redux sync
A store enhancer for Redux which allows to mirror one store as a part of another and keeps them in sync.
About
Although one of the core principles of Redux is referring to a single source of truth, depending on the application its not always possible. Environments like iframes
or Web Worker
can't share code directly as they are running in a different context. While its possible to use just one store and pass each data manually, another approach is to run these stores separate and automatically synchronize data changes. This allows to access the data at a single store and invoke changes across scope boundaries (e.g. triggering an action which changes the values outside will also transfer the value into the iframe
store).
Usage
Install
npm install --save redux-sync
General setup
1.) Configure the redux store which should be synchronized
import syncStore from 'redux-sync'
// optional
const middlewares = [
thunkMiddleware,
loggerMiddleware
]
const finalCreateStore = compose(
applyMiddleware(...middlewares),
syncStore()
)(createStore)
const store = finalCreateStore(reducer, initialState)
2.) Configure the redux store which should should contain the synchronized store
import syncStore from 'redux-sync'
// optional
const middlewares = [
thunkMiddleware,
loggerMiddleware
]
const finalCreateStore = compose(
applyMiddleware(...middlewares),
syncStore('subtree')
)(createStore)
const store = finalCreateStore(reducer, initialState)
FAQ
Copying data from one store to another sounds like redundant data management...
No question it is, but regarding the aspect that this module is meant to ease the state management between different context it provides the advantages of completeness and performance.
Why not using redux persist ?
Indeed using a layer in between which can be accessed from both could be used, but the focus is actual not on persistence (you could still do it if you like) but on communication and optimized synchronization of substates.
TODO
- add example
- checkout more lightweight alternatives (current total ~ 39kb), e.g. for patching
- support include/exclude filter options
- integrate webworker and websocket adapter
Development
To build your own version run npm run dev
for development (incl. watch) or npm run build
for production (minified).