npm package: @geut/staty
Description: Build a proxy-state from plain objects
Author: GEUT
Latest version: 2.3.3

# staty

Build a proxy-state from plain objects. With automatic rollback to previous state in case of errors.

![Test Status](https://github.com/geut/staty/actions/workflows/test.yml/badge.svg)
[![Coverage](https://raw.githubusercontent.com/geut/staty/gh-pages/badges/coverage.svg?raw=true)](https://geut.github.io/staty/)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![standard-readme compliant](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)

[![Made by GEUT][geut-badge]][geut-url]

## Install

```bash
$ npm install @geut/staty
```

## Usage

```javascript
import { staty, subscribe, snapshot, action } from '@geut/staty'

const state = staty({
  count: 0
})

console.log(snapshot(state)) // => { count: 0 }

subscribe(state, () => {
  console.log(state) // => { count: 1 }
})

subscribe(state, () => {
  console.log(state.count) // => 1
}, { props: 'count' })

// filter multiple values
subscribe(state, () => {
  console.log(state.count) // => 1
}, { props: ['count'] })

state.count++

try {
  action(() => {
    state.count = 100
    throw new Error('ups')
  })
} catch (err) {
  console.log(err) // => ups
}

// rollback to the last good state and subscriptions won't be trigger it
state.count // => 1
```

## API

<!-- apiness/api -->

#### `staty(target, opts?) => any`

Creates a new proxy-state

- `target: any`
- `opts?: Object = {}`
  - `onReadOnly?: (target: any, prop: any, value: any) => {}` global handle for readonly snapshot errors
  - `onAction?: (state: Proxy, actionName: any) => {}` global subscription to run before every action. Create a state is also an action so it will run on every staty({}) call.

#### `listeners(state) => ListenersReport`

Get subscription listeners count

- `state: any`

#### `subscribe(state, handler, opts?) => UnsubscribeFunction`

Subscribe for changes in the state

- `state: any`
- `handler: () => void`
- `opts?: Object = {}`
  - `props?: string | string[]` props to subscribe
  - `filter?: (actionName: string) => boolean` subscribe only for specific action names
  - `batch?: boolean = false` execute in batch turning the subscription into async. **Required before=false**
  - `autorun?: boolean` run immediately. **Required before=false**
  - `before?: boolean` run before the other subscriptions and after the action finish. Good place to validate your state.  **Required batch=false && autorun=false**

#### `ref(value, mapSnapshot?, cache?) => any`

Add a ref to another object

- `value: any`
- `mapSnapshot?: (ref: any) => any`
- `cache?: boolean = false` enable cache for snapshots

#### `action(handler, actionName) => void`

Create a action

- `handler: Function`
- `actionName: string`

#### `snapshot(state, prop?) => any`

Creates a snapshot of the state

- `state: any`
- `prop?: (string | Array<string>)`

## Issues

:bug: If you found an issue we encourage you to report it on [github](https://github.com/geut/staty/issues). Please specify your OS and the actions to reproduce it.

## Contributing

:busts_in_silhouette: Ideas and contributions to the project are welcome. You must follow this [guideline](https://github.com/geut/staty/blob/main/CONTRIBUTING.md).

## License

MIT © A [**GEUT**](http://geutstudio.com/) project

[geut-url]: https://geutstudio.com

[geut-badge]: https://img.shields.io/badge/Made%20By-GEUT-4f5186?style=for-the-badge&link=https://geutstudio.com&labelColor=white&logo=data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wAARCABAAEADASIAAhEBAxEB/8QAGwAAAgIDAQAAAAAAAAAAAAAABAYDBQACBwH/xAA0EAACAQMBBAcGBgMAAAAAAAABAgMABBEFBhIhQRMiMVFhgcEUIzJxkbFCUmKh0fAkcuH/xAAYAQADAQEAAAAAAAAAAAAAAAABAwQCAP/EACARAAMAAwACAgMAAAAAAAAAAAABAgMRIRIxBEEiM1H/2gAMAwEAAhEDEQA/AOgVlau6xoXdgqqMkk8AKV9U2oYs0WngBRw6VhxPyFamXXoDeiz1PUbmzuujQIUKgjIqGLXnz72FSO9TikfVbi6uXWSSaWRuzixNBx3VzCepNIvgTw+hpjwv+iGr3tM6xa30F2PdP1uangRRNc70f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