Code Monkey home page Code Monkey logo

state-model's Introduction

state-model

一个无框架依赖、非集中式的状态管理

  • 支持单独多处引入
  • 可以接收外部参数(仅第一次传入有效)
  • 状态不需要作为参数传递

注:框架支持,可在编写自定义Model时,手动支持(见下文Vue 的响应式支持)

1、Api

createModel

自定义 model 的包装器

限制(Typescript 类型)自定义 model 返回值需要有 state,同时获取返回值,方便使用时候的取值类型推导

props

使用 自定义Model 时传入的外部参数

注意:props 只会在第一次传入的时候生效

// 第一次传入是 { a: 'a' }
const countModel = useCountModel({ a: 'a' });

// 第二次传入,无效,此时 useCountModel 内部的props参数仍然是第一次传入的 { a: 'a' }
const countModel2 = useCountModel({ a: 'aaaa' });

onChange

自定义 modelsetState 的时候调用

参数 params:

  • { key: string, value: any, params?: any }

仅允许通过这种方式更新state,外部直接修改state无效

onStateChange (beta)

自定义监听 onChange 的回调

需要在自定义 modelsetState 的时候调用一次 onChange 方法

参数 params

  • callback: ({ key: string, value: any, params?: any }): void;
  • deps: string[]
const countModel = useCountModel();
console.log(countModel.state.count); // 0

// 调用下面 setCount 之后执行回调函数
useCountModel.onStateChange(
  ({ key, value }) => {
    console.log(key, value); // 'count', 6
  },
  ['count']
);

countModel.setCount(6);
console.log(countModel.state.count); // 6
console.log(useCountModel.data.state.count); // 6

2、编写自定义 Model

建议将自定义 model 写在 createModel 里面,这样使用 Typescript 时回调函数会有类型; 见 src/_example/useTestModel.ts

// __test/useCountModel.js
const zrModel = require('../dist/state-model.umd.js');

const { createModel } = zrModel;

/**
 * useCountModel
 * @param {*} param0
 * @returns
 */

module.exports = createModel(function useCountModel({ props, onChange }) {
  const state = {
    count: 0,
  };

  console.log(props);

  function setCount(newCount) {
    onChange({ key: 'count', value: newCount });
  }

  return {
    state,
    setCount,
  };
});

3、使用自定义 Model

  • 正常引用: testModel.state
  • 特殊引用: useTestModel.data.state
import useTestModel from './useTestModel';

const testModel = useTestModel();
// 正常引用
console.log(testModel.state.count); // 0
// 不需要执行 `useTestModel()` 时引用
console.log(useTestModel.data.state.count); // 0

testModel.setCount(6);

testModel.state.count = 8; // 无效
console.log(testModel.state.count); // 6
console.log(useTestModel.data.state.count); // 6

4、框架的响应式支持

编写自定义model时候,使用框架提供的相应的响应式方法

Vue 响应式支持

  • [Vue2.6+] Vue.observable :让一个对象可响应。Vue 内部会用它来处理 data 函数返回的对象。

  • [Vue3.x+] Vue.reactive:返回对象的响应式副本

例:

function useCountModel({ onChange }) {
  const state = {
    count: 0,
  };

  function setCount(count) {
    onChange('count', count);
  }

  // 如在setCount之后,外部引用state.count的地方就会响应式更新了
  Vue.observable(state);

  return {
    state,
    setCount,
  };
}

React 响应式支持

TODO,不清楚有没有类似上面 Vue 的外部响应式支持。。。

其他

TODO,不清楚有没有类似上面 Vue 的外部响应式支持。。。

5、参考

  • hox.js: 基于 React Hooks 状态管理器

6、最后

项目基于 rollup-starter-lib 创建

state-model's People

Contributors

zero9527 avatar

Watchers

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