@replygirl/vanity
Version:
simple, reactive state management for any framework using @vue/reactivity
61 lines (41 loc) • 2.37 kB
Markdown
# `vanity`
simple, reactive state management for any framework using [`@vue/reactivity`](https://github.com/vuejs/vue-next/tree/master/packages/reactivity#readme)
  [](https://libraries.io/npm/@replygirl%2Fvanity) [](https://codeclimate.com/github/replygirl/vanity/maintainability) [](https://codeclimate.com/github/replygirl/vanity/test_coverage) [](https://github.com/replygirl/vanity/issues) [](https://github.com/replygirl/vanity/pulls)
`vanity` keeps business logic simple by abstracting it away from application state management. It can be used as a lightweight global store, or paired with a state management pattern like [`vuex`](https://github.com/vuejs/vuex) to better separate concerns.
While `vanity` was created with [`vue`](https://github.com/vuejs/vue-next) in mind, it works out of the box with other frameworks like [`svelte`](https://github.com/sveltejs/svelte), [`react`](https://github.com/facebook/react), and [`angular`](https://github.com/angular/angular).
## Installation
```bash
yarn add @replygirl/vanity
```
## Usage
```ts
// Creating a service
import { createService } from '@replygirl/vanity'
type State = {
bar: boolean | null
}
export default createService<State>({
name: 'foo',
baseState: {
bar: null
},
methods: ({ clear, commit, state }) => ({
setBar(bar: boolean) {
if (bar !== state.bar) commit({ bar })
},
reset() {
clear()
}
})
})
```
```ts
// Consuming a service
import { watchEffect } from '@vue/reactivity'
import foo from '@/services/foo'
watchEffect(() => console.info(foo.bar)) // null
foo.setBar(true) // true
foo.reset() // null
```
## License
[ISC (c) 2020 replygirl](https://github.com/replygirl/vanity/blob/main/LICENSE.md)