
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
redux-detector
Advanced tools
Redux enhancer for pure detection of state changes.
Warning: API is not stable yet, will be from version 1.0
Redux Detector requires Redux 3.1.0 or later.
npm install --save redux-detector
This assumes that you’re using npm package manager with a module bundler like Webpack or Browserify to consume CommonJS modules.
To enable Redux Detector, use createDetectableStore
:
import { createDetectableStore } from 'redux-detector';
import rootReducer from './reducers/index';
import rootDetector from './detectors/index';
const store = createDetectableStore(
rootReducer,
rootDetector
);
or if you have more complicated store creation, use createStore
with createDetectorEnhancer
:
import { createStore } from 'redux';
import { createDetectorEnhancer } from 'redux-detector';
import rootReducer from './reducers/index';
import rootDetector from './detectors/index';
const store = createStore(
rootReducer,
createDetectorEnhancer(rootDetector)
);
Redux Detector enhancer allows you to use state changes detectors with redux. Detector is a simple and pure function which compares two states and returns action or list of actions for some states configurations. It can be used for reacting on particular state transitions.
type Detector<S> = <A extends Action>(prevState: S | undefined, nextState: S) => A | A[] | void
For example detector that checks if number of rows exceed 100 looks like this:
function rowsLimitExceededDetector(prevState, nextState) {
if (prevState.rows.length <= 100 && nextState.rows.length > 100) {
return { type: ROWS_LIMIT_EXCEEDED };
}
}
You can also return array of actions to dispatch them.
Thanks to detectors purity they are predictable and easy to test. There is no problem with features like time-travel, etc.
Redux store can handle only one detector (and one reducer). But don't worry - you can combine them. To do this, use
combineDetectors
function.
// ./detectors/rootDetector.js
import { combineDetectors } from 'redux-detector';
import { fooDetector } from './fooDetector';
import { barDetector } from './barDetector';
export default combineDetectors(fooDetector, barDetector);
Detectors by default operates on global state, but if you want to make some reusable detector that is not binded to global state,
you can use mountDetector
function. With factory pattern it becomes very elastic.
// ./detectors/limitExceedDetector.js
export function createLimitExceedDetector(limit, action) {
return function limitExceedDetector(prevState, nextState) {
if (prevState <= limit && nextState > limit) {
return action;
}
}
}
// ./detectors/rowsLimitExceedDetector.js
import { mountDetector } from 'redux-detector';
import { createLimitExceeedDetector } from './limitExceedDetector';
export const rowsLimitExceedDetector = mountDetector(
state => state.rows.length,
createLimitExceedDetector(100, ROWS_LIMIT_EXCEEDED)
);
Of course examples above are very trivial, but you can use it to solve more common problems like keeping state consistent (you can for example schedule resource fetch if some parameters changed).
Redux Detector provides replaceDetector
method on DetectableStore
interface (store created by Redux Detector). It's similar to
replaceReducer
- it changes detector and dispatches { type: '@@detector/INIT' }
.
If you are using TypeScript, you don't have to install typings - they are provided in npm package.
MIT
FAQs
Redux enhancer for pure detection of state changes.
The npm package redux-detector receives a total of 877 weekly downloads. As such, redux-detector popularity was classified as not popular.
We found that redux-detector 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
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.