Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chopped-redux

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chopped-redux

A very small Flux implementation based on [@gaearon](https://github.com/gaearon) [Redux](https://github.com/gaearon/redux).

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

Chopped Redux

A very small Flux implementation based on @gaearon Redux.

The idea is to provide a minimal, solid base without the React glue (you have to do that yourself). No hot reloading or time travelling (yet?).

Install

With npm do:

npm install chopped-redux --save

Example

Stores

Stores are pure functions, just like in Redux. They don't hold the state and don't emit events either. 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
  }
}

Actions

Actions should have the following signature, always receiving a dispatch function as last argument. (There's a utility wrapper function to make this easy)


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
  })
}

Constants (for action types)

Yep.

module.exports = {
  INCREMENT_COUNTER: 'INCREMENT_COUNTER',
  DECREMENT_COUNTER: 'DECREMENT_COUNTER',
}

Make it work together

var fluxFactory = require('chopped-redux')
var wrap = fluxFactory.wrapActionCreators

var stores = {
  counter: require('./stores/counter'),
  beep: function (state, action) {
    return action.type === SOME_ACTION_TYPE_CONSTANT
      ? 'boop'
      : state || undefined
  }
}

var actionCreators = {
  counter: require('./actions/counter'),
}

var flux = fluxFactory(stores, { counter: 1 })
var actions = wrap(actionCreators, flux.getDispatcher())

var unsubscribe = flux.subscribe(function () {
  console.log(flux.getState())  
})

actions.increment()
// => { counter: 2 }

unsubscribe()

License

MIT

FAQs

Package last updated on 25 Jun 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc