applefreak / lil-store

Super duper tiny state management

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lil-store

My naive approach to create a state management library.

Inspired by Redux, Vuex and Flux architecture.

Install

npm install lil-store

How to use

In all seriousness, just read the code. It's less than 30 lines!

const { createStore } = require('./index')

const reducer = (store, action, payload) => {
  if (!store) store = 0 // this is how you define your initial state

  switch(action) { // define how your state will change according to action
    case 'incr':
    store++
    break
    case 'decr':
    store--
    break
    case 'add':
    store += payload
    break
  }

  return store
}

const store = createStore(reducer)

store.subscribe(state => console.log(state))

store.commit('add', 5) // prints out 5
store.commit('incr') // prints out 6
store.commit('incr') // prints out 7
store.commit('decr') // prints out 6

In Browser

Through UNPKG:

<script src="https://unpkg.com/lil-store"></script>

An object named lilStore is exposed to the global Window object.

You can then access createStore like so:

<script>
var createStore = lilStore.createStore;
</script>

Why

State management is not a React/Vue only need!

This library (more like code snippet) allows you to easily create a reactive store for whatever needs you have!

Roadmap

  • Export module for browser use
  • Publish that to unpkg.com or something
  • Add tests
  • Implement TravisCI
  • Implement code coverage

License

MIT

About

Super duper tiny state management


Languages

Language:JavaScript 100.0%