Simple state container for your vqua components
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec/support
.gitignore
.npmignore
README.md
lerna-debug.log
logo.jpg
package-lock.json
package.json

README.md

alt tag

Memorux

Minimalistic state container for your vqua components

Example

Step 1. Declare your store

class Cat {

  constructor() {

    this.state = {
      say: null
    }

  }

  dispatch(state, action) {

    switch (action.name) {

      case 'update': {

        return action.data

        break

      }

      default: {

        return action.data

      }

    }
  }

}

Step 2. Assign your stores

const memorux = require('memorux')

const memorux = new Memorux({ Cat })

memorux.store // => { Cat: { say: null } }

Step 3. Dispatch action

memorux.dispatch('Cat#update', { say: 'meow' })

// or

memorux.dispatch({
  name: 'Cat#update',
  data: { say: 'meow' }
})

// or

memorux.dispatch([
  {
    name: 'Cat#update',
    data: { say: 'meow' }
  },
])

Step 4. Listen changes

memorux.onChange(store =>

  // store => { Cat: { say: 'meow' } }

)

Delayed actions

just return a promise like callback from your dispatch method

class Cat {

  constructor() {

    this.state = {
      say: null
    }

  }

  dispatch(state, action) {

    switch (action.name) {

      case 'update': {

        return (resolve, reject) => {

          setImmediate(() => {

            resolve(action.data)

          })

        }

        break

      }

      default: {

        return action.data

      }

    }
  }

}

Also, if you dispatch a few delayed actions you may need an event ending all of the actions, you can do so

memorux.dispatch([

  // delayed action
  // delayed action
  // delayed action

]).then((store) => {

  // do something with store
  // after all actions ready

})