Engine, handle dispatched events and maintain state
Part of the fluxgear
project
import createEngine from 'fluxgear-engine';
const engine = createEngine({
*transformer(event) {
switch(event.type) {
case 'BUTTON_CLICKED':
yield { type: 'EVENT_LOGGED', msg: 'clicked' };
yield { type: 'COUNTER_INCREMENTED' };
default:
}
},
consumer(event) {
switch(event.type) {
case 'EVENT_LOGGED':
console.log(event.msg);
default:
}
},
reducer(state, event) {
switch(event.type) {
case 'COUNTER_INCREMENTED':
return {
...state,
counter: state.counter + 1,
};
default:
return state;
}
},
initialState: { counter: 1 },
});
{
mapper,
transformer,
consumer,
reducer,
initialState,
getApiProps,
getDependencies,
}
A function taking an event and returning an iterable collection of messages.
{
*transformer(
event,
{
getApiProps,
getState,
}
) {
yield message;
}
}
A function taking an event and producing side effects.
Note: the dispatch
option must not be called synchronously.
{
consumer(
event,
{
dispatch,
getApiProps,
getDependencies,
getState,
}
) {
sideEffect();
}
}
A pure function taking the current state and an event, and returning the new state.
{
reducer(
state,
event,
{
getApiProps,
}
) {
return state;
}
}
The initial value of the state.
A function provided as an option to the transformer
, to the consumer
and to the reducer
.
A function provided as an option to the consumer
.
{
dispatch,
getState,
subscribe,
}