brianneisler / duxtape

Module and utility framework for redux
http://duxtape.org
MIT License
16 stars 0 forks source link

duxtape

"You can never have enough duxtape for redux."

Module and util framework for redux.

Benefits

Build Status

npm version
Build Status
NPM

Documentation

Full API documentation - Learn about each method

Install

npm install --save duxtape

Usage

import o from 'duxtape'
import { reducer } from 'duxtape/modules'

const { decrement, increment } = o.createActions({
  DECREMENT: amount => amount,
  INCREMENT: amount => amount
})

const build = o.compose(
  o.setName('inc'), // sets base state path to 'inc'
  o.withActions({
    decrement,
    increment
  }),
  o.defaultState({
    counter: 0
  }),
  o.withReducer(() => o.handleActions({
    DECREMENT: (state, action) => ({
      counter: state.counter - action.payload
    }),
    INCREMENT: (state, action) => ({
      counter: state.counter + action.payload
    })
  }),
  o.withHooks({
    init: (state) => console.log('module initialized'),
    receiveState: (nextState, state) => console.log('module state received:', nextState)
  })
)

const module = build()

// Order of modules matters, they will be processed in order given
const store = o.createStore([
  reducer,
  module
])

// listen for state changes on all of store
store.subscribe((state) => {
  console.log('store state changed')
  console.log('store state:', state)
})

// listen for state changes on module
module.subscribe((state) => {
  console.log('module state changed')
  console.log('module state:', state)
})

// old school dispatch
store.dispatch({ type: 'INCREMENT', payload: 1 })

// dispatch with action creator
store.dispatch(increment(2))

// dispatch via module
module.decrement(3)

// get store state
console.log(store.getState())

// get module state
console.log(module.getState())