Giter Club home page Giter Club logo

stateful's Introduction

@nkjmsss/stateful

This is a vuex-like state manager.

installation

$ npm i --save @nkjmsss/stateful

usage

NOTE: You must define state and mutations.

import Stateful from '@nkjmsss/stateful'

class Store extends Stateful<Store['state'], Store['mutations']> {
  protected state = {
    foo: 1,
    bar: 'bar',
    baz: {
      key1: 0,
      key2: true,
    },
  }

  protected mutations = {
    incrementFoo: () => {
      this.state.foo++
    },

    setBar: (payload: Store['state']['bar']) => {
      this.state.bar = payload
    },

    optionalMutation: (payload?: number) => {
      if (payload === void 0) {
        // do some staff
      } else {
        // do other staff
      }
    },
  }

  constructor() {
    super()
    this.init()
  }
}

const StoreInstance = new Store()

Then, you can use methods like below

// get params
const bar = StoreInstance.getState.bar // -> string
const foo = StoreInstance.getState.foo // -> number
const baz = StoreInstance.getState.baz
// ->
// DeepReadonlyObject<{
//   key1: number;
//   key2: boolean;
// }

// direct mutation (impossible)
// StoreInstance.getState.bar = 'foo' // -> Cannot assign to 'bar' because it is a read-only property.ts(2540)
// StoreInstance.getState.baz.key1 = 1 // -> Cannot assign to 'key1' because it is a read-only property.ts(2540)

// mutating using mutation
StoreInstance.commit('incrementFoo')
StoreInstance.commit('setBar', 'qux')
StoreInstance.commit('optionalMutation')
StoreInstance.commit('optionalMutation', 12)

// watch state
StoreInstance.on('updated', store => {
  console.log(store.getState)
})

stateful's People

Watchers

James Cloos 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.