From f2da91c59114ccb8654f76a33e8817fd1e7839e0 Mon Sep 17 00:00:00 2001 From: DD Liu <liudi@media.mit.edu> Date: Wed, 10 May 2017 14:10:17 -0400 Subject: [PATCH] throttle requests and fix code for merging new and old state --- src/reducers/monitors.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/reducers/monitors.js b/src/reducers/monitors.js index 46cbb2c75..0e9832fcd 100644 --- a/src/reducers/monitors.js +++ b/src/reducers/monitors.js @@ -7,7 +7,22 @@ const reducer = function (state, action) { switch (action.type) { // Adds or updates monitors case UPDATE_MONITORS: - return [...action.monitors, ...state]; + let newState = [...state]; + let updated = false; + for (let i = 0; i < action.monitors.length; i++) { + for (let j = 0; j < state.length; j++) { + if (action.monitors[i].id == state[j].id) { + newState[j] = action.monitors[i]; + updated = true; + continue; + } + } + if (!updated) { + newState.push(action.monitors[i]); + } + updated = false; + } + return newState; default: return state; } @@ -18,7 +33,7 @@ reducer.updateMonitors = function (monitors) { type: UPDATE_MONITORS, monitors: monitors, meta: { - throttle: 30 + throttle: 100 } }; }; -- GitLab