State management with Redux is really nice. But it requires switch statements and works poorly with deeply nested states. I also find the terminology very confusing.
- To manage state with a simple PubSub pattern
- For State to return the whole state or just a chunk of state (just what you need).
- Message filtering can be applied without a
switch
statement (you create your own event types) - To allow for manipulation of deeply nested state properties through use of strings
- Maintain a small size. Currently it's 6kb minified, <2kb gzipped!
- Demos
- Installation
- Instantiation
- Options
- Initialization
- State Methods
- Event Methods
- State Events
- Custom Events
- Updates to Come
- Demo showing State object changing. The module can be observed in the console
- Demo with React and VanillaJS
- new demos forth-coming with React, Vanilla JS, jQuery
npm install substate --save
- copy and paste from
index.js
into a<script>
or external js file
SubState is a class so you call it like so
import SubState from 'substate';
Then you instantiate it as such
var s = new SubState();
Substate accepts an options object as an optional parameter. These are the possible options
Option | Desc | Default |
---|---|---|
name | name of the instance | 'SubStateInstance' |
currentState | index of state to start on | 0 |
stateStorage | array of all the states | [ ] |
saveOnChange | save state to localStorage on change | null |
pullFromLocal | pull currentState from localStorage on initialization | null |
state | object containing the initial state | null |
to initialize the class call
instance.init()
@param
optional method parameter@param*
required method parameter
Method | Desc | Returns |
---|---|---|
getState | get a state @param* - index of state needed |
state |
getcurrentState | get the current state | current state object |
getProp | get a prop from current state @param* - string path to prop |
property you request |
changeState | change the version of the state @param* - {requestedState: index of state, action: (optional name of event to emit)} |
emits action parameter event or 'STATE_CHANGED' event with the new current state |
saveState | save stateStorage array to localStorage. Will use instance param of name | emits 'STATE_SAVED' |
removeSavedState | removed state from LocalStorage | emits 'STATE_REMOVED_SAVED_STATE' |
resetState | resets the stateStorage array to an empty array |
emits 'STATE_RESET' |
@param
optional method parameter@param*
required method parameter@param[num]
order of method parameter
Method | Desc |
---|---|
on | @param1* STRING of event name to listen to. @param2* FUNC handler to execute when this event you listen to happens |
off | @param1* STRING of event name to remove handler from. @param2* FUNC to remove from the execution queue |
emit | @param1* STRING event name @param2 object of data to pass into your handler event from 'on' method |
Event | Desc | Returns |
---|---|---|
'UPDATE_STATE' | updates the entire state with the object passed in | updated state |
'UPDATE_CHUNK' | updates part of a state, this does not iterate over the entire state object | the data passed in |
'CHANGE_STATE' | fires changeState method above requires same @param s |
emits 'STATE_CHANGED' |
note: the object of data that is passed, cannot have a key called 'type'
Method | Event | Custom Event | Next |
---|---|---|---|
emit | 'UPDATE_CHUNK' or 'UPDATE_STATE' | @param2 is an object: {type: 'MY_CUSTOM_EVENT'} |
Will update/change state. The type property will then be emitted so you can listen to it like SubStateInstance.on('MY_CUSTOM_EVENT', func) |
Basically to utilitze a custom event, you still need to use UPDATE_STATE
/UPDATE_CHUNK
but the data object needs a type
with an event name you want the State to emit when updated
Documentation on Custom Events- Example Code for all Events and Custom Events
- documentation on special array methods for state
- change 'action' nomenclature to 'event'
- Add in comments and console.logs and remove them with UglifyJS
- Have only production version with dev version not published to npm