Giter Club home page Giter Club logo

dutier's Introduction

Dutier is a small (1kb) and simple state management solution for Javascript applications.

npm package CDN

Influences

It evolves on the ideas of Redux.

Getting Started

Install

Features

  • small 1kb minified
  • simple, small learning curve
  • no dependencies
  • promise based
  • inspired by Redux
  • tiny API.

React Example:

React with Dutier

With dutier your Actions are pure functions that just returns a payload information about how to work with the state, and the dispatch method always return new values based on your initial state without change them.

import { createStore, getState, dispatch } from 'dutier'

// sets your initial application state
createStore({ count: 1 }, reducer)

// your action
function increment( value ) {
  return { type: 'INCREMENT', value }
}

// your reducer function
function reducer( state, { type, value } ) {
  switch (type) {
    case 'INCREMENT':
      return Object.assign({}, state, { count: state.count + value })
    default:
      return state  
  }
}
 
 // dispatch actions to change the state
dispatch( increment(1) )
  .then( ({ type, state }) => console.log( state, getState() )) // { count: 2 },{ count: 2 }
 

The Gist

The application state is stored in an object tree inside a single store. Your actions will only dispatch information about how work with the state and then return new state values based on your state.

That's it!

import { createStore, subscribe, dispatch, getState } from 'dutier'

/**
 * Set the initial store state in a single object.
 */
createStore( { count: 1 } )

/**
 * Actions are pure functions that return a payload
 */
function increment( value ) {
  return { type: 'INCREMENT', value }
}

/**
 * Simple Reducer
 * As Redux, the only way to change the state tree is to emit an action, an 
 * object describing what happened.
 */
function reducer( state, { type, value } ) {
  switch (type) {
    case 'INCREMENT':
      return { count: value }
    default:
      return state  
  }
}
    
/**
 * You can use subscribe() to update your UI in response to actions
 */
 componentWillMount() {
  this.unsubscribe = subscribe( { type, state } ) => {
    console.log('Reducer new state value ', state, 'Store state: ', getState())
  })
 }


/**
 * Use dispatch to return new values based on the state
 * tree. dispatch returns a Promise with your action type and
 * the actual state
 */
dispatch(increment( 1 ))
 .then( ({ type, state }) => {
   console.log(`The value is: ${getState().count}`) // 2
 })
 
dispatch(increment( 1 )) // 3
dispatch(increment( 1 )) // 4

getState().count // 4

Simple and efficient API.

Dispatch

  • Trigger an action to do something with the state. A Promise will be returned,
    that contains your action payload
/**
 * @name dispatch
 * @description Trigger some action.
 * @param { Function } The function that return your action payload
 * @return {Promise} Return a Promise with the action payload
 */

// On your component
import {dispatch} from 'dutier'

// You can receive the response of your action and do something, or not.
// If you want, you can chain the dispatch Promises.
dispatch( increment(1) )
  .then( ({ type, state }) => {
    console.log(type, state)
  })

Actions

  • Actions are payloads of information that send data from your application to your store. They are the only source of information for the store. You send them to the store using dispatch().
function increment( value ) {
  return { type: 'INCREMENT', value }
}

Store State

  • Set the initial Application store state.
/**
 * @name createStore
 * @description Set your initial Application store state
 * @param { Object } state Your application state data
 * @param { Function } reducers Your store reducers
 */
 
import { createStore } from 'dutier'

createStore( { count: 1 [, ...reducers] )

Getting the store state

  • Get a state value from your store
/**
 * @name getState
 * @description Get the state value
 */
 
import {getState} from 'dutier'

getState() // returns a copy of your store state { count: 1 }

Combine

  • Combine your store reducers
/**
 * @name combine
 * @param {Function} Your reducers functions
 * Each reducer function receives your actual store state
 * as first argument
 */
 
import { combine } from 'dutier'

function reducer( state, { type, value } ) {
  switch (type) {
    case 'INCREMENT':
      return Object.assign( {}, state, { count: state.count + value })
    default:
      return state  
  }
}

combine( reducer [, ...reducers ])

Subscribe

  • Subscribe to your store to update your UI in response to actions.
  • Unsubscribe when unmounted.
/**
 * @name subscribe
 * @description Bind your UI to changes in your store state.
 * @param { Component } BindComponent The UI element that the handler function will be bound to.
 * @param { handler } handler A callback function that will be triggered in response to actions.
 */
 
import {subscribe, getState} from 'dutier'
  
  componentWillMount(){
     // Subscribe to changes on your store, do something with the value.
     this.unsubscribe = subscribe(( { type, state } ) => {
       this.setState( { count: getState().count } )
     })
  }
  
  componentWillUnmount() {
    this.unsubscribe()
  }

That's all folks!

License

MIT License.

dutier's People

Contributors

luisvinicius167 avatar sunpietro avatar theolampert avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.