Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
redux-state-sync
Advanced tools
A lightweight middleware to sync your redux state across browser tabs. It will listen to the Broadcast Channel and dispatch exactly the same actions dispatched in other tabs to keep the redux state in sync.
In redux state sync 1.0, communicate between tabs are fully rely on local storage which means the actions send to other tabs are actually saved in the local storage. We also need to use JSON.stringify and JSON.parse while we saving or retrieving it and all localStorage calls are synchronous. All of this could cause performance problems.
Thanks to BroadcastChannel, we now have a more efficient way to communicate between tabs instead of using any type of local storage. However, Not all the browsers support BroadcastChannel API for now. So I used pubkey's BroadcastChannel to find the best way to communicate between tabs for redux-state-sync 2.0. BroadcastChannel will make sure that the communication between tabs always works.
Install with npm.
npm install --save redux-state-sync
Install with yarn
yarn add redux-state-sync
Install with npm.
npm install --save-dev @types/redux-state-sync
Install with yarn
yarn add --dev @types/redux-state-sync
Types are defined here
Create the state sync middleware with config:
import { createStore, applyMiddleware } from 'redux'
import { createStateSyncMiddleware } from 'redux-state-sync';
const config = {
// TOGGLE_TODO will not be triggered in other tabs
blacklist: ['TOGGLE_TODO'],
}
const middlewares = [
createStateSyncMiddleware(config),
];
const store = createStore(rootReducer, {}, applyMiddleware(...middlewares));
Init new tabs with existing state:
import { createStore, applyMiddleware } from 'redux'
import { createStateSyncMiddleware, initStateWithPrevTab } from 'redux-state-sync';
const config = {
// TOGGLE_TODO will not be triggered in other tabs
blacklist: ['TOGGLE_TODO'],
}
const middlewares = [
createStateSyncMiddleware(config),
];
const store = createStore(rootReducer, {}, applyMiddleware(...middlewares));
// init state with other tabs
initStateWithPrevTab(store);
withReduxStateSync
import { withReduxStateSync } from 'redux-state-sync'
const rootReducer = combineReducers({
todos,
visibilityFilter
})
export default withReduxStateSync(rootReducer)
redux-persist
, because you will always inite your app with the state in the storage. However, if you don't want to persist the state in the storage and still want to init new tabs with opening tabs' state, you can follow the example above.Unique name for Broadcast Channel
type: String
default: "redux_state_sync"
const config = {
channel: 'my_broadcast_channel',
}
const middlewares = [
createStateSyncMiddleware(config),
];
A function to let you filter the actions as you wanted.
type: Function
default: null
const config = {
// All actions will be triggered in other tabs except 'TOGGLE_TODO'
predicate: actionType => actionType !== 'TOGGLE_TODO'
}
const middlewares = [
createStateSyncMiddleware(config),
];
A list of action types that you don't want to be triggered in other tabs.
type: ArrayOf(<String>)
default: []
const config = {
// All actions will be triggered in other tabs except 'TOGGLE_TODO'
blacklist: ['TOGGLE_TODO']
}
const middlewares = [
createStateSyncMiddleware(config),
];
Only actions in this list will be triggered in other tabs.
type: ArrayOf(<String>)
default: []
const config = {
// Only 'TOGGLE_TODO' will be triggered in other tabs
whitelist: ['TOGGLE_TODO']
}
const middlewares = [
createStateSyncMiddleware(config),
];
Redux-state-sync is using BroadcastChannel to comunicate between tabs. broadcastChannelOption is the option passed to broadcastChannel when we creating the channel.
type: Object
default: null
const config = {
// Only 'TOGGLE_TODO' will be triggered in other tabs
whitelist: ['TOGGLE_TODO'],
// enforce a type, oneOf['native', 'idb', 'localstorage', 'node']
broadcastChannelOption: { type: 'localstorage' },
}
const middlewares = [
createStateSyncMiddleware(config),
];
Prepare state for sending to channel. Will be helpful when using Immutable.js
type: Function
default: state => state
const config = {
// Map immutable object to js
prepareState: state => state.toJS()
}
const middlewares = [
createStateSyncMiddleware(config),
];
FAQs
A middleware for redux to sync state in different tabs
The npm package redux-state-sync receives a total of 23,003 weekly downloads. As such, redux-state-sync popularity was classified as popular.
We found that redux-state-sync demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.