This repository has been archived on 2025-10-29. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
gekko/web/state/reduceState.js
2021-06-06 20:25:14 +02:00

67 lines
1.3 KiB
JavaScript

// Redux/vuex inspired reducer, reduces an event into a gekko state.
// NOTE: this is used by the backend as well as the frontend.
const skipInitialEvents = ['marketUpdate'];
const skipLatestEvents = ['marketStart', 'stratWarmupCompleted'];
const trackAllEvents = ['tradeCompleted', 'advice', 'roundtrip'];
const reduce = (state, event) => {
const type = event.type;
const payload = event.payload;
state = {
...state,
latestUpdate: new Date()
}
if(trackAllEvents.includes(type)) {
if(!state.events[type]) {
state = {
...state,
events: {
...state.events,
[type]: [ payload ]
}
}
} else {
state = {
...state,
events: {
...state.events,
[type]: [ ...state.events[type], payload ]
}
}
}
}
if(!state.events.initial[type] && !skipInitialEvents.includes(type)) {
state = {
...state,
events: {
...state.events,
initial: {
...state.events.initial,
[type]: payload
}
}
}
}
if(!skipLatestEvents.includes(type)) {
state = {
...state,
events: {
...state.events,
latest: {
...state.events.latest,
[type]: payload
}
}
}
}
return state;
}
// export default reduce;
module.exports = reduce;