litsy is a minimal node web data state machine
npm install litsySet a function call on state change \> Set a StateName/State pair \> Trigger all function calls \> In function call, update local states as necessary
litsy also allows you to specify if you'd like a certain dataStore to be persistent (useful for saving web-app auth tokens or loginState etc.) or non-persistent (useful for keeping track of current app state which should be let go after a user leaves the app).
$ yarn add litsy or $ npm i litsy
Once you've installed the dependency, you can get started with whatever you want. litsy's main reason for existence is to simplify the process of sharing data between different parts of an application whilst simultaneously allowing any objects dependent on certain data to be notified of a change. litsy makes all of this super simple.
```
import { Store } from 'litsy'``
...``
let myStore = new Store({
storeName: "awesome_app",
persist: true
});stateNameSubscribe/Unsubscribe to a stateName
$3
A is the name assigned to a state you would like to watch the contents of. One example of organizing stateNames is by assigning names relative to the content you expect in any given state such as
````
stateName: "data.profile_section.user_name"
value: "John Doe"
The way to subscribe to a stateName (even before you have instantiated the stateName along with the respective state) is as follows:
``
myStore.subscribe(stateName, subscriberName, callBackFn);`
Where a callBackFn is applied to a subscriberName. When the state associated with the stateName is changed, all subscribers get notice of a stateChange having occurred. At this point you can re-render a react-component, feed some new data, whatever you want to do. An example would be:`
myStore.subscribe("data.profile_section.user_name", "usernameLabel", () => {
this.forceUpdate.bind(this);
};`
It is possible to have the same subscriber subscribed to multiple states and it's also possible for multiple subscribers to be subscribed to the same state. Keep in mind that any subscriberEvents should not depend on one another to function.$3
to unsubscribe, simply just`
myStore.unsubscribe(stateName, subscriberName);
myStore.setState(stateName, value);
`
setting a state always notifies subscribers and saves the state to either localStorage (if data is persistent) or sessionStorage (in the case that the store is a non-persistent store).$3
getting a state is easy as well, all you have to do is
`
let result = myStore.getState(stateName);
``