Security News
UK Officials Consider Banning Ransomware Payments from Public Entities
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
chopped-redux
Advanced tools
A very small Flux implementation based on @gaearon Redux, mainly inspired by this and this.
I recommend you try out Redux and go through its Github issues as it's a great source of knowledge and inspiration.
The idea here is to provide a minimal, solid Flux base without the React glue (you have to do that yourself).
This project follows SemVer. 1.0 doesn't mean it's stable or production-ready.
With npm
do:
npm install chopped-redux --save
Function|Object
These are your storesMixed
Anything you want to hold your state inObject
A flux
instanceThe reducer
function should have this signature:
function (state, action) {
// do something with state depending on the action type,
// ideally generating a fresh new value
return state
}
What happens internally on every action dispatch is basically this:
state = reducer(state, action)
If you pass an object with reducer functions, a new function will be created which will map the reduced state of every function to a key on the root state object. Like this:
// Your stores
{
foo: [Function],
bar: [Function],
baz: [Function]
}
// The root state inside the `flux` instance
{
foo: {...} // the reduced state from `stores.foo`
bar: {...} // the reduced state from `stores.bar`
baz: {...} // the reduced state from `stores.baz`
}
// And you could access those like this
flux.getState().foo
Object
dispatch
function bound to the flux
instance. Literally this: return this.dispatch.bind(this)
Object
Your stateFunction
A callback that gets fired after every state updateFunction
A function to remove the listenerObject
Your action creators in an objectFunction
The dispatch function to bind to your action creators. You can get this from flux.getDispatcher()
Object
Your action creators bound to the dispatcherThis is a helper function, so instead of writing this:
var actionsCreators = {
exampleAction: function (foo) {
return {
type: constants.EXAMPLE_TYPE,
value: foo
}
}
}
flux.dispatch(actionCreators.exampleAction('bar'))
you can do this:
var actionsCreators = {
exampleAction: function (foo, dispatch) {
return dispatch({
type: constants.EXAMPLE_TYPE,
value: foo
})
}
}
var wrap = fluxFactory.wrapActionCreators
var actions = wrap(actionCreators, flux.getDispatcher())
actions.exampleAction('bar') // dispatches!
and have the action automatically dispatched.
Stores are pure functions, just like in Redux. They don't hold the state and don't emit events either (that's why in Redux they're called Reducers; I prefer to keep calling them Stores). They just receive the state, update it, and return the new state.
var actionTypes = require('../constants/action-types')
var initialState = 0
module.exports = function (state, action) {
state = state || initialState
switch (action.type) {
case actionTypes.INCREMENT_COUNTER:
return state + 1
break
case actionTypes.DECREMENT_COUNTER:
return state - 1
break
default:
return state
}
}
Action creators are functions that yield an action object (or payload in vanilla Flux terminology). They can simply return that object, or if you need to do async operations, you can pass in a dispatch callback and fire it passing in the action object. If the latter is the case, always pass the dispatch
function as the last argument, so you can use the utility wrapper function (wrapActionCreators
).
Further reading: The Evolution of Flux Frameworks.
var actionTypes = require('../constants/action-types')
exports.increment = function (dispatch) {
return dispatch({
type: actionTypes.INCREMENT_COUNTER
})
}
exports.decrement = function (dispatch) {
return dispatch({
type: actionTypes.DECREMENT_COUNTER
})
}
Yep.
module.exports = {
INCREMENT_COUNTER: 'INCREMENT_COUNTER',
DECREMENT_COUNTER: 'DECREMENT_COUNTER',
}
var fluxFactory = require('chopped-redux')
var wrap = fluxFactory.wrapActionCreators
// Define stores and action creators
var stores = {
counter: require('./stores/counter')
}
var actionCreators = {
counter: require('./actions/counter')
}
// Create a flux instance passing in the stores and initial state
var flux = fluxFactory(stores, { counter: 1 })
// Bind action creators to the dispatcher
var actions = wrap(actionCreators, flux.getDispatcher())
// Subscribe a callback to state updates
var unsubscribe = flux.subscribe(function () {
console.log(flux.getState())
})
// Trigger an action: this dispatches the action,
// the state tree in the flux instance goes through the store function(s),
// and listener callbacks fire
actions.increment()
// => { counter: 2 }
unsubscribe()
MIT
FAQs
An implementation of @gaearon Redux
The npm package chopped-redux receives a total of 7 weekly downloads. As such, chopped-redux popularity was classified as not popular.
We found that chopped-redux 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.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.