install it
> npm install @fedeghe/pangjsdefine an asynchronous reducer as:
const reducer = async (oldState, action, payload) => {
const res = await fetch(targetUrl);
//
// your updates
//
return newState
}get a store and use it:
const PANGjs = require('@fedeghe/pangjs')
const store = PANGjs.getStore( reducer, initState );
// stage the results only internally
store.stage({
type: 'ADD',
payload: { number: 4 }
})
// here we get staged state
.then(console.log);
// make all staged changes effective
store.dispatch()
// here we get the state
.then(console.log);alternatively one single call just adding true as second parameter also dispatch:
store.stage({
type: 'ADD',
payload: { number: 4 }
}, true /* autoDispatch */)
.then(console.log); // here we get the statealternatively it is possible to directly dispatch the action:
store.dispatch({
type: 'ADD',
payload: { number: 4 }},
)
.then(console.log); // here we get the stateParameters:
-
reducer:
thereducerfunction expected to have the following signature:(oldState, actionType, payload) => Promisewhere the returning promise resolves with the updated state.
-
initialState:
optional object representing the initial state needed; when not provided it will be just an empty object. -
config: this is an optional object allowing to change some default behaviors:- maxElements (default 1):
by default no history is available, but if here we pass a number bigger than one, for example 5 then we can navigate the state back up to 5 steps using the
movefunction to the store. - check (default no check):
here we can pass a synchrhonous function expected to have to following signature:
allowing to prevent a state change under some circumstances; that decision can be made based on the
( state, currentAction, previousAction, payload ) -> <Boolean>state, the ongoingcurrentActionwhich we might block, thepreviousActionand the currentpayload. Only if we returntrue, the reducer action will be run.
- maxElements (default 1):
by default no history is available, but if here we pass a number bigger than one, for example 5 then we can navigate the state back up to 5 steps using the
Returns:
the store instance
Throws:
[ERROR] Reducer should return something! in case the passed reducer is not a function
Synchronous function to combine 2 or more reducers.
Parameters:
- [reducer, ...]: two or more reducers to be combined
Returns:
the resulting combined reducer
Throws:
[ERROR] Reducer must be a function! in case one of the parameters is not a function
Parameters:
- toBeChecked: what needs to be checked if it is a PANGjs store or not
Every store obtained invoking successfully PANGjs.getStore exposes the following:
returns the state
stage or stage&dispatch returning a promise resolving with new state (staged or not);
Parameters:
- action:
{ type: <String>, payload: <Object> }
- autoDispatch
<Boolean>defaultfalse
dispatch all the staged changes.
Only this operations calls subscribers.
Optionally it can recieve an action and that will be equivalent to stage and dispatch:
s.stage(action).then(() => s.dispatch())
act as
s.stage(action, true)
and as
s.dispatch(action))
allows to register a subscribing function that will be invoked everytime the state changes (dispatched)
returns: the unsubscribing function
replace the store reducer
in case the history is active allows to move in the states
reset the store to its initial state
