(n.) Something that is the absolute core and driving force behind something.
Simple state management inspired on Redux.
Motivation: I've been using Redux in several projects recently and don't get me wrong, I love it, but I've found that at some point, if my app scales, my Redux files (actions/reducers/store) start to get messy and a bit annoying to handle. So inspired by the basic concepts of Redux, I decided to create this library where I've simplified that workflow and aim to have a better and easier file structure. I hope you like it and any feedback or collaboration is more than welcome.
Install
npm install thrux --save
or with yarn
yarn add thrux
API
Dictionaries
; const state = console rawValuedata console; ;
createDict(dispatcher, map, error)
Create the dictionary of methods that will be used for each action.
Param | Type | Description |
---|---|---|
dispatcher | Function | Update the current state. This could return an Object or a Promise and update the state async. |
map | Function | (optional) A map function to sanitize the value handle by the dispatcher function. |
error | Function | (optional) Error handler. |
; const postJson = method: 'POST' headers: 'Content-Type': 'application/json' body: JSON; const counter = INCREASE: DECREASE: const user = SIGN_IN: ;
Initialization
You can define an INIT action with a function that sets the initial value of your state after register
; const counter = INIT : INCREASE: DECREASE:
init()
Initialize Thrux store.
Param | Type | Description |
---|---|---|
options | Object | Initialize any module: { middlewares: [], dicts: {}, observers: {}, store: {} } |
; ;
initState(key)
Triggers INIT
action (a.k.a. initialize the state manually)
Param | Type | Description |
---|---|---|
key | String or [Array of Strings] | (optional) State(s) that want to initialize |
; ;;
register(dictionaries)
Register your dictionaries.
Param | Type | Description |
---|---|---|
dictionaries | Object | List of states and their respective dictionaries |
; ;
dispatch(stateAction, data)
Dispatch the action that will update your state.
Param | Type | Description |
---|---|---|
stateAction | String or [Array of Strings] | String(s) that represents the state(s) and the action(s) that you want to dispatch: 'state:ACTION' |
data | Any | (optional) Whatever data your dispatcher function is prepared to handle |
; ; ; ;
state(stateKey)
returns: [Object]
Retrieve the state value.
Param | Type | Description |
---|---|---|
stateKey | String or [Array of Strings] | (optional) String(s) that represents the state(s) |
; const allStates = ; const someStates = ; const userStates = ;
observe(stateKey, listener)
Observe when the state changed.
Param | Type | Description |
---|---|---|
stateKey | String | String that represents the state |
listener | Function | Function that gets trigger when the state changes |
; ;
Micro Observer
You can observe specific parts of the state for changes
; ;;
removeObserver(stateKey, listener)
Remove an observe listener.
Param | Type | Description |
---|---|---|
stateKey | String | String that represents the state |
listener | Function | Function that gets trigger when the state changes |
; const logProfile = console; ; ; // logProfile won't trigger
clearObservers(stateKey)
Remove all observe listeners.
Param | Type | Description |
---|---|---|
stateKey | String or [Array of Strings] | String that represents the state |
; ;;
addMiddleware(middleware)
Add some middleware function. It won't modified the state.
Param | Type | Description |
---|---|---|
middleware | Function or [Array of Functions] | Function(s) that trigger when the state changes with the following params: {state, action, prev, payload, next} |
; // Add logger;;
getActions(stateKeys)
Retrieve the state(s) actions keys.
Param | Type | Description |
---|---|---|
stateKey | String or [Array of Strings] | (optional) String(s) that represents the state(s) |
; const actions = ; // ['user:INIT', 'user:SIGN_IN']
Clear()
Clear Thrux store.
; ; // { test:0 }; // { }
reset()
Reset Thrux all your modules to your initial values.
; ; // { test:0 }; // { test:0, counter: 0 }; // { test:0 }