1
0
mirror of https://github.com/yldio/copilot.git synced 2024-12-26 03:40:04 +02:00
copilot/legacy/spikes/graphs-matrix/nvd3/client/actions.js
Sérgio Ramos 8295bd6882 chore: initial lerna setup
this shall be a progressive process
2017-05-25 10:56:50 +01:00

65 lines
1.2 KiB
JavaScript

const take = require('lodash.take');
const actions = {
'UPDATE_STATS': (state, action) => {
const data = [action.payload].concat(state[action.subscription] || []);
return {
...state,
[action.subscription]: take(data, state.windowSize)
};
}
};
module.exports = (state, action) => {
return !actions[action.type] ? state : actions[action.type](state, action);
};
module.exports.subscribe = (id) => (dispatch, getState) => {
const {
ws
} = getState();
const p = new Promise((resolve, reject) => {
ws.subscribe(`/stats/${id}`, (update, flag) => {
dispatch({
type: 'UPDATE_STATS',
payload: update,
subscription: id
});
}, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
return dispatch({
action: 'SUBSCRIBE',
payload: p
});
};
module.exports.unsubscribe = (id) => (dispatch, getState) => {
const {
ws
} = getState();
const p = new Promise((resolve, reject) => {
ws.unsubscribe(`/stats/${id}`, null, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
return dispatch({
action: 'UNSUBSCRIBE',
payload: p
});
};