# statemgr

> Just a simple state manager

Latest version **1.1.1** (published 2019-01-17) · 0 weekly downloads

## Install

```sh
npm install statemgr
pnpm add statemgr
yarn add statemgr
bun add statemgr
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2019-01-17 |
| First published | 2019-01-07 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Teddy Gandon |
| Maintainers | teddygandon |
| Keywords | state, manager |

## Links

- npm: https://www.npmjs.com/package/statemgr
- Repository: https://github.com/TeddyGandon/statemgr
- Homepage: https://github.com/TeddyGandon/statemgr#readme
- Issues: https://github.com/TeddyGandon/statemgr/issues
- npm.io page: https://npm.io/package/statemgr

## Dependencies (1)

- [obsrvble](https://npm.io/package/obsrvble.md) ^1.1.4

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 1.1.1 (latest) — 2019-01-17
- 1.1.0 — 2019-01-17
- 1.0.1 — 2019-01-07
- 1.0.0 — 2019-01-07

## README

![Logo](https://raw.githubusercontent.com/TeddyGandon/icons/master/st.svg?sanitize=true)
# STATEMGR

Just a simple state manager for small JS projects.

# Usage

`npm i statemgr`

# API doc

## Statemgr

### set
`set(property, value)`
Stores a property into the state manager

### get
`get(property)`
Returns a stored property

### setReducer
`setReducer(dispatcher)`
Set a reducer (a method that will be called for complex set). The reducer will be called with 2 arguments: the parameters passed to the reducer call & the `set` method of the state manager.

### reduce
`reduce(...params)`
Call the reducer.

# Example

Set & get a value:
```js
import Statemgr from "statemgr";

Statemgr.set('foo', 'bar');
console.log(Statemgr.get('foo')); //bar
```

Subscribe to some changes:
```js
import Statemgr from "statemgr";
import {Observer} from "obsrvble";

Statemgr.subscribe(new Observer(
    'foo',
    params => {
        // Do something here
        console.log(params[0]); // bar
    }
));
Statemgr.set('foo', 'bar');
```
Call the reducer:
```js
import Statemgr from "statemgr";
import {Observer} from "obsrvble";

Statemgr.subscribe(new Observer(
    'total',
    params => {
        // Do something here
        console.log(params[0]);
    }
));

Statemgr.set('total', 0);
Statemgr.setReducer((params, set) => {
    if (params[0] === 'add') {
        set('total', Statemgr.get('total') + 1);
    }
    if (params[0] === 'sub') {
        set('total', Statemgr.get('total') - 1);
    }
});

Statemgr.reduce('add'); //1
Statemgr.reduce('add'); //2
Statemgr.reduce('sub'); //1
```

---
_Source: https://npm.io/package/statemgr · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
