All Projects → unadlib → Usm

unadlib / Usm

Licence: mit
🏖 A concise & flexible state model for Redux/MobX/Vuex, etc.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Usm

Alldemo
🍑 2020全栈学习Demo大合集 包含最新 hooks TS 等 还有umi+dva,数据可视化等实战项目 (持续更新中)
Stars: ✭ 189 (-30%)
Mutual labels:  vuex, mobx
Xsm
State Management made eXtraordinarily simple and effective for Angular, React, and Vue
Stars: ✭ 138 (-48.89%)
Mutual labels:  vuex, mobx
react-mobx-router4-axios
react + less + webapck config + mobx(store)+ axios + router4
Stars: ✭ 24 (-91.11%)
Mutual labels:  mobx
Vue Home
🏠 A simple project(Vue Community SPA) which bases on vue+vue-cli+vue-router+axios+ scss.
Stars: ✭ 256 (-5.19%)
Mutual labels:  vuex
react-douban
A React project
Stars: ✭ 21 (-92.22%)
Mutual labels:  mobx
tinylog-ui
实时日志分析系统后台数据管理系统
Stars: ✭ 45 (-83.33%)
Mutual labels:  mobx
mobx-collection-store
Data collection store for MobX
Stars: ✭ 36 (-86.67%)
Mutual labels:  mobx
fetchx
Beautiful way to fetch data in React
Stars: ✭ 71 (-73.7%)
Mutual labels:  mobx
Vue Inspector
Vue.js Inspector for Mobile Devices
Stars: ✭ 266 (-1.48%)
Mutual labels:  vuex
dashboard
📺🐝OpenPitrix UI
Stars: ✭ 29 (-89.26%)
Mutual labels:  mobx
Movie Trailer
🍿Vue3 + TypeScript开发的电影预告片webAPP,可以查看正在热映与即将上映的电影信息和短片
Stars: ✭ 253 (-6.3%)
Mutual labels:  vuex
react-native-mobx-feathers
A basic App using react-navigation + mobx + feathers
Stars: ✭ 31 (-88.52%)
Mutual labels:  mobx
okdux
redux made ok 👌
Stars: ✭ 16 (-94.07%)
Mutual labels:  mobx
kami
🍰 Kami is mx-space's web frontend theme. Cute and lovely.
Stars: ✭ 92 (-65.93%)
Mutual labels:  mobx
mobx
Код учебного курса “MobX & React” на YouTube-канале webDev (https://tinyurl.com/vt3tk6vy)
Stars: ✭ 23 (-91.48%)
Mutual labels:  mobx
Column
Vue3.0+Typescript+axios+bootstrap+源码注释/博客专栏作品
Stars: ✭ 261 (-3.33%)
Mutual labels:  vuex
react-mobx-router5
React components for routing solution using router5 and mobx
Stars: ✭ 58 (-78.52%)
Mutual labels:  mobx
mobx-form
Declarative, complex forms with Mobx/React with lots of dynamic/imperative hooks
Stars: ✭ 29 (-89.26%)
Mutual labels:  mobx
web-starter-kit
An opinionated starter kit with styled-system, graphql-hooks, mobx and nextjs (PWA)
Stars: ✭ 17 (-93.7%)
Mutual labels:  mobx
Vue Mpvue Chatrobot
㊙A chat robot for web & Wechat producted by vue+mpvue+nodejs.
Stars: ✭ 269 (-0.37%)
Mutual labels:  vuex

USM

Node CI npm

USM is a universal state modular library, supports Redux(4.x), MobX(6.x), Vuex(4.x) and Angular(2.0+).

Support

Libraries/Frameworks None Redux MobX Vuex Angular2+
Status

Installation

To install usm:

yarn add usm # npm install --save usm

And if you want to use Redux/MobX/Vuex, you just install usm-redux/usm-mobx/usm-vuex.

Usage

  • Use @state to decorate a module state.

  • Use @action to decorate a module method for state changes.

  • Use createStore to create a store.

import { state, action, createStore } from 'usm';
// You can also use `usm-redux`, `usm-mobx`, or`usm-vuex`.

class Counter {
  @state
  count = { sum: 0 };

  @action
  increase() {
    this.count.sum += 1;
  }
}

const counter = new Counter();

const store = createStore({
  modules: [counter],
});

counter.increase();

const newState = Object.values(store.getState())[0] as Counter;
expect(newState.count).toEqual({ sum: 1 });

Examples

APIs

@state

Define a shared state for a module, and you can use @state for decoration. When use usm-redux, the state is not allowed to be undefined.

For example,

class Counter {
  @state
  number = 0;
}

@action

All operations that change state must be in a method decorated by @action.

For example,

class Counter {
  @state
  number = 0;

  @action
  increase() {
    this.number += 1;
  }
}

@computed/@computed()

It is used for computing derived data.

  • When use usm or usm-redux, you should use @computed(depsCallback), The return value of the depsCallback is an array of dependent value collections that tells the module that its getter will recompute when there is a change in any of the values in the value collections:

For example,

class Counter {
  @state
  count = { sum: 0 };

  @state
  number = 0;

  @action
  increase() {
    this.number += 1;
  }

  @computed((that) => [that.count.sum, that.number])
  get sum() {
    return this.count.sum + this.number;
  }
}
  • When use usm-mobx or usm-vuex, you just use @computed, Since it is an observable model, its dependency collection is automatic:

For example,

class Counter {
  @state
  count = { sum: 0 };

  @state
  number = 0;

  @action
  increase() {
    this.number += 1;
  }

- @computed((that) => [that.count.sum, that.number])
+ @computed
  get sum() {
    return this.count.sum + this.number;
  }
}

createStore()

Creates a usm store that holds the complete shared state.

Arguments

  • options(object)
    • modules(array): an array with all modules instances
    • [strict] (boolean): enable strict mode
  • [preloadedState] (any): preloaded state
  • [plugins/middleware] (any[]): vuex's plugins or redux's middleware

For example,

class Counter {
  @state
  number = 0;

  @action
  increase() {
    this.number += 1;
  }
}

const counter = new Counter();

const store = createStore({
  modules: [counter],
});

subscribe()

You can use subscribe() to subscribe state changes in any class module.

For example,

class Counter {
  constructor() {
    subscribe(this, () => {
      //
    });
  }

  @state
  count = { sum: 0 };
}

watch()

You can use watch() to observe a specific state changes in any class module.

For example,

class Counter {
  constructor() {
    watch(
      this,
      () => this.count.sum,
      (newValue, oldValue) => {
        //
      }
    );
  }

  @state
  count = { sum: 0 };
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].