Build a proxy-state from plain objects
npm install @geut/statyBuild a proxy-state from plain objects. With automatic rollback to previous state in case of errors.
!Test Status



[![Made by GEUT][geut-badge]][geut-url]
``bash`
$ npm install @geut/staty
`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
`
#### staty(target, opts?) => any
Creates a new proxy-state
- target: anyopts?: Object = {}
- onReadOnly?: (target: any, prop: any, value: any) => {}
- global handle for readonly snapshot errorsonAction?: (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: anyhandler: () => void
- opts?: Object = {}
- props?: string | string[]
- props to subscribefilter?: (actionName: string) => boolean
- subscribe only for specific action namesbatch?: boolean = false
- execute in batch turning the subscription into async. Required before=falseautorun?: boolean
- run immediately. Required before=falsebefore?: 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: anymapSnapshot?: (ref: any) => any
- cache?: boolean = false
- enable cache for snapshots
#### action(handler, actionName) => void
Create a action
- handler: FunctionactionName: string
-
#### snapshot(state, prop?) => any
Creates a snapshot of the state
- state: anyprop?: (string | Array
-
:bug: If you found an issue we encourage you to report it on github. Please specify your OS and the actions to reproduce it.
:busts_in_silhouette: Ideas and contributions to the project are welcome. You must follow this guideline.
MIT © A GEUT 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+iGr3tM6xa30F2PdP1uangRRNc70fUbi4JLIVaPskXgM/wA076Ze+2W+WwJF4MPWlNaemajI2/GvYbWVlZQHCptZqLNKLGJsKoDSY5nkKorKzlvrlYIRlm5nsA7zWX8pnv55SfikJ/emPZGDcs7m6CguTuL5DPrVf64Me2F2mzNhAg6ZTO/MsSB9BW15s1pt1GVEPRHvQ+hqbTNT9sZ0kCpIOIA5ij5ZEijaSRgqqMkmpVkb7sMuWtoV73S49L3I4B7kjq57c881BZ6vFpuoKjq7dIvYBw8PtUOqX1xcSxoJXw8mQuewVW3vX1eFR+Fcn96OLVvpFzz8kM020kp4QwIvixzVpot5Je2bSTEFw5HAY7qUKadnIymm7x/G5I+3pTskzM8G4rqq6JGpI8E1wi8HR2H0NT7P6rcRKUEzYR9/czgEf0VabV2JgvhdKPdzdvg399aVG37K4Es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