react 前端框架如何驱动企业数字化转型与创新发展
738
2022-10-27
OverVue一个为Vue程序提供“异步优先”状态管理的库
OverVue
OverVue is a stream-based persistent state management library for Vue built on RxJS observables.
While Vuex provides a robust option for handling state in Vue applications, persistence requires third-party plugin support. The OverVue state management library includes persisted state out of the box.
OverVue leverages an RxJS Observable stream to manage synchronous and asynchronous updates to state and easily integrates with Vue applications. With OverVue, all actions are emitted to our single source of truth, the motherstream, along with any Observables contained therein. This allows users to easily assimilate Observables into their Vue applications while maintaining the Flux-based architecture most conducive to Vue.
Disclaimer
OverVue is in the early stages of development. We welcome any constructive feedback and encourage users to open issues or submit pull requests in order to contribute to the ongoing evolution of OverVue.
Getting Started
npm install --save overvue-rx
Create a central store
Creating your store is a simple two-step process with OverVue:
In your store file, import OverVue and connect it using the Vue.use() global method.
import Vue from 'vue';import { Overvue, Store } from 'overvue-rx';Vue.use(Overvue);
Next, in the file containing your root Vue instantiation, pass your mutate methods into createStateStream and subscribe to the store. Be sure to import the relevant files and set the store as a property in your root Vue instantiation. This makes the store accessible in all components.
import store from './store';import mutate from './mutate';store.createStateStream(mutate).subscribe();const app = new Vue({ el: '#App', store, router, render: h => h(Container),});
Dispatch actions
All dispatched actions will be emitted and mapped into OverVue's motherstream. Users simply call the dispatchAction method from the store and pass in a callback function, which will return an object containing type and payload to be used as state update instructions to the user-defined mutators. The following is a simple example of how to declare an action in OverVue:
export const commitUsername = store.dispatchAction(payload => ({ type: 'SET_USERNAME', payload,}));
In this example, commitUsername can be easily imported into any file requiring it and utilized in the same way one would use actions in any Flux-based state management library.
Commit mutations
As in other Flux-based state management libraries, state is never directly effected by dispatched actions. Rather, actions commit mutators which mutate state and save the new state object in session storage, allowing it to persist through page reloads. This allows actions to perform complex asynchronous tasks without the risk of state being inappropriately changed.
This is an example of how a committed mutate method will handle a dispatched action:
export default function mutate(state, action) { switch (action.type) { case 'SET_USERNAME': state.username = action.payload.username; return state;};
And that's it!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~