Mockstate is a centralized state management for Javascript applications. It provides more control of your state application with a simple and efficiently API and keep the state safe, when the user connection goes offline the state is saved in localStorage to be picked up when the connection back.
React.js Todo ➞
💁 See the
Demos
React.js Todo with Mockstate ➞
Preact.js + Mockstate ➞
Influences
Mockstate provides more control of your application state. It's envolve ideas of Redux and Flux, but explores all power of Promises, doing the work with async action easy.
Getting Started
Install
- Npm:
npm install mockstate
- Bower:
bower install mockstate
- CDN:
https://unpkg.com/mockstate@0.0.6
Why you should be using Mockstate?
- More control of you application state.
- Safe State.
- It's pure flux, the flow is unidirectional.
- Tiny API.
- Actively maintained and being used in production.
The Gist
The application state is stored in an object tree inside a single store.
The only way to change the state tree is to emit an action, that always return a Promise.
You can receive the result of your action using .then
after your dispatch
call.
That's it!
; /** * Set your store state in a single object. */ /** * Set your store state in a single object, * that always receive the store state as first argument. */; /** * You can use subscribe() to update your UI in response to state changes. * `${this}` can be your UI component, where the subscribe handler will be applied. */ /** * The only way to mutate the internal state is to dispatch an action. * You can receive the response of your action and do something, or not. *///1;//2;//3
Simple and efficiently API.
Dispatch
- Trigger some action for do something. A Promise will be returned, that contain an Object with the
keysaction
andvalue
of your correspondent action response.
/** * @name dispatch * @description Trigger some action. * @param * @param { arguments } Arguments You can pass the arguments after the actionName * @return * of the action and the action name. * {value, action} = data; */ // On your component; // You can receive the response of your action and do something, or not.// If you whant, you can chaining the dispatch Promises.
Actions
- Set your actions functions. Your actions always needs to return a value and receive the state as first argument.
/** * @name setActions * @description Set the actions functions. * @param * @param { any } args Other Arguments */ // actions/index.js; ;
Store State
- Set the application Store state
/** * @name setState * @description Set you application store state * @param */ // store/index.js; ;
Getting the Store State
- Get a state value of your store
/** * @name getState * @description Get the state value * @param */ // store/index.js; let count = ;
Middleware
- Set a middleware function, that will be triggered after the action calls.
/** * @name middleware * @description Get the state value * @param * receives the data of your action and the all state of your store application. */ // state/index.js;
Emit
- Tell to store that something are happend and the handler function will be triggerd.
/** * @name emit * @description Tell to store that something happend, * and the handler function will be called. * @return void **/ // state/index.js; ;
Subscribe/Unsubscribe
- Subscribe some UI Component for trigger the handler function when some action are trigger.
- Unsubscribe when you don't wnat to trigger the handler function.
/** * @name subscribe * @description Subscribe some UI Component for trigger the handler function when some action calls. * @param { object } BindComponent The UI element that the handler function will be applied. * @param { handler } handler Your function that will be triggered when some state change. */ // components/app/index.js; { // when some state change, do something. ; } { // remove this component for observe the changes on the state }
License
MIT License.