Bug 1554883 - Refactor requests reducer to remove disabled complexity eslint rule. r=pbro
authorArun Kumar Mohan <arunmohandm@gmail.com>
Sat, 27 Jul 2019 00:47:00 +0000
changeset 485266 0a1a438c807e04f0c660bb12a711257e5a96fed8
parent 485265 ded87cc3f3eed779fd0b525921f76dc5f6818216
child 485267 502912f6dd40158f94f551a6fd54f4cc1c5eb802
push id91219
push useraciure@mozilla.com
push dateTue, 30 Jul 2019 07:57:27 +0000
treeherderautoland@0a1a438c807e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspbro
bugs1554883
milestone70.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1554883 - Refactor requests reducer to remove disabled complexity eslint rule. r=pbro Differential Revision: https://phabricator.services.mozilla.com/D39472
devtools/client/netmonitor/src/reducers/requests.js
--- a/devtools/client/netmonitor/src/reducers/requests.js
+++ b/devtools/client/netmonitor/src/reducers/requests.js
@@ -41,72 +41,26 @@ function Requests() {
     lastEndedMillis: -Infinity,
   };
 }
 
 /**
  * This reducer is responsible for maintaining list of request
  * within the Network panel.
  */
-/* eslint-disable complexity */
 function requestsReducer(state = Requests(), action) {
   switch (action.type) {
     // Appending new request into the list/map.
     case ADD_REQUEST: {
-      const nextState = { ...state };
-
-      const newRequest = {
-        id: action.id,
-        ...action.data,
-        urlDetails: getUrlDetails(action.data.url),
-      };
-
-      nextState.requests = mapSet(state.requests, newRequest.id, newRequest);
-
-      // Update the started/ended timestamps.
-      const { startedMillis } = action.data;
-      if (startedMillis < state.firstStartedMillis) {
-        nextState.firstStartedMillis = startedMillis;
-      }
-      if (startedMillis > state.lastEndedMillis) {
-        nextState.lastEndedMillis = startedMillis;
-      }
-
-      // Select the request if it was preselected and there is no other selection.
-      if (state.preselectedId && state.preselectedId === action.id) {
-        nextState.selectedId = state.selectedId || state.preselectedId;
-        nextState.preselectedId = null;
-      }
-
-      return nextState;
+      return addRequest(state, action);
     }
 
     // Update an existing request (with received data).
     case UPDATE_REQUEST: {
-      const { requests, lastEndedMillis } = state;
-
-      let request = requests.get(action.id);
-      if (!request) {
-        return state;
-      }
-
-      request = {
-        ...request,
-        ...processNetworkUpdates(action.data, request),
-      };
-      const requestEndTime =
-        request.startedMillis +
-        (request.eventTimings ? request.eventTimings.totalTime : 0);
-
-      return {
-        ...state,
-        requests: mapSet(state.requests, action.id, request),
-        lastEndedMillis:
-          requestEndTime > lastEndedMillis ? requestEndTime : lastEndedMillis,
-      };
+      return updateRequest(state, action);
     }
 
     // Remove all requests in the list. Create fresh new state
     // object, but keep value of the `recording` field.
     case CLEAR_REQUESTS: {
       return {
         ...Requests(),
         recording: state.recording,
@@ -130,18 +84,17 @@ function requestsReducer(state = Request
       return cloneRequest(state, action.id);
     }
 
     case CLONE_SELECTED_REQUEST: {
       return cloneRequest(state, state.selectedId);
     }
 
     case RIGHT_CLICK_REQUEST: {
-      const { requests } = state;
-      const clickedRequest = requests.get(action.id);
+      const clickedRequest = state.requests.get(action.id);
       return {
         ...state,
         clickedRequest,
       };
     }
 
     // Removing temporary cloned request (created for re-send, but canceled).
     case REMOVE_SELECTED_CUSTOM_REQUEST: {
@@ -179,20 +132,72 @@ function requestsReducer(state = Request
 
       return state;
     }
 
     default:
       return state;
   }
 }
-/* eslint-enable complexity */
 
 // Helpers
 
+function addRequest(state, action) {
+  const nextState = { ...state };
+
+  const newRequest = {
+    id: action.id,
+    ...action.data,
+    urlDetails: getUrlDetails(action.data.url),
+  };
+
+  nextState.requests = mapSet(state.requests, newRequest.id, newRequest);
+
+  // Update the started/ended timestamps.
+  const { startedMillis } = action.data;
+  if (startedMillis < state.firstStartedMillis) {
+    nextState.firstStartedMillis = startedMillis;
+  }
+  if (startedMillis > state.lastEndedMillis) {
+    nextState.lastEndedMillis = startedMillis;
+  }
+
+  // Select the request if it was preselected and there is no other selection.
+  if (state.preselectedId && state.preselectedId === action.id) {
+    nextState.selectedId = state.selectedId || state.preselectedId;
+    nextState.preselectedId = null;
+  }
+
+  return nextState;
+}
+
+function updateRequest(state, action) {
+  const { requests, lastEndedMillis } = state;
+
+  let request = requests.get(action.id);
+  if (!request) {
+    return state;
+  }
+
+  request = {
+    ...request,
+    ...processNetworkUpdates(action.data, request),
+  };
+  const requestEndTime =
+    request.startedMillis +
+    (request.eventTimings ? request.eventTimings.totalTime : 0);
+
+  return {
+    ...state,
+    requests: mapSet(state.requests, action.id, request),
+    lastEndedMillis:
+      requestEndTime > lastEndedMillis ? requestEndTime : lastEndedMillis,
+  };
+}
+
 function cloneRequest(state, id) {
   const { requests } = state;
 
   if (!id) {
     return state;
   }
 
   const clonedRequest = requests.get(id);