vanix

Vuex like Flux Library

Usage no npm install needed!

<script type="module">
  import vanix from 'https://cdn.skypack.dev/vanix';
</script>

README

Vanix

Vuex like state management library.
Not support component.
Support ES6 or later. Please transpile if want supporting ES5.

install

$ npm install vanix --save-dev

How to use

store.js

import Vanix from 'vanix'

const state = {
  alpha: 'Alpha',
  beta: 'Beta',
  charlie: 'Charlie'
}

const mutations = {
  setCharlie(state, data) {
    state.charlie = data
  }
}

const actions = {
  async asyncSetCharlie(context, data) {
    // const { commit, state, getters, dispatch } = context
    await new Promise((resolve) => {
      setTimeout(() => {
        context.commit('setCharlie', data)
        resolve()
      }, 1000)
    })
  }
}

const vanix = new Vanix({ state, mutations, actions })

export const store = vanix.create()

main.js

import { store } from './store'

// get
console.log(store.state.alpha) // Alpha

// set (commit)
store.commit('setCharlie', 'New Charlie')

// async commit
store.dispatch('asyncSetCharlie', 'Super Charlie')

// observe
const storeObserveObj = store.observe({
  beta(state) {
    console.log(state.beta) // Beta
  }
})

// unobserve
store.unobserve(storeObserveObj)

Destroy

import Vanix from 'vanix'

const vanix = new Vanix()
const store = vanix.create()

vanix.destroy()

License

MIT