Bug 1418969 - Remove helper API from requests reducer; r=Honza draft
authorabhinav <abhinav.koppula@gmail.com>
Sun, 21 Jan 2018 00:08:21 +0530
changeset 723178 d22f069cc520f6816d4de1632425ae58046fcd14
parent 723064 3d23e6d98a09a3395bf2b0d9cb03dd4be358c715
child 746784 eeb0ffd296d6690ba3a927b3486ebcf62be151f8
push id96346
push userbmo:abhinav.koppula@gmail.com
push dateMon, 22 Jan 2018 17:27:29 +0000
reviewersHonza
bugs1418969
milestone60.0a1
Bug 1418969 - Remove helper API from requests reducer; r=Honza MozReview-Commit-ID: 8r7rHH23sqC
devtools/client/netmonitor/src/components/MonitorPanel.js
devtools/client/netmonitor/src/components/StatisticsPanel.js
devtools/client/netmonitor/src/reducers/requests.js
devtools/client/netmonitor/src/selectors/requests.js
--- a/devtools/client/netmonitor/src/components/MonitorPanel.js
+++ b/devtools/client/netmonitor/src/components/MonitorPanel.js
@@ -131,17 +131,17 @@ class MonitorPanel extends Component {
         }),
       )
     );
   }
 }
 
 module.exports = connect(
   (state) => ({
-    isEmpty: state.requests.requests.isEmpty(),
+    isEmpty: state.requests.requests.size == 0,
     networkDetailsOpen: state.ui.networkDetailsOpen,
     request: getSelectedRequest(state),
     selectedRequestVisible: isSelectedRequestVisible(state),
   }),
   (dispatch) => ({
     openNetworkDetails: (open) => dispatch(Actions.openNetworkDetails(open)),
     updateRequest: (id, data, batch) => dispatch(Actions.updateRequest(id, data, batch)),
   }),
--- a/devtools/client/netmonitor/src/components/StatisticsPanel.js
+++ b/devtools/client/netmonitor/src/components/StatisticsPanel.js
@@ -330,16 +330,16 @@ class StatisticsPanel extends Component 
         ),
       )
     );
   }
 }
 
 module.exports = connect(
   (state) => ({
-    requests: state.requests.requests.valueSeq(),
+    requests: [...state.requests.requests.values()],
   }),
   (dispatch, props) => ({
     closeStatistics: () => dispatch(Actions.openStatistics(props.connector, false)),
     enableRequestFilterTypeOnly: (label) =>
       dispatch(Actions.enableRequestFilterTypeOnly(label)),
   })
 )(StatisticsPanel);
--- a/devtools/client/netmonitor/src/reducers/requests.js
+++ b/devtools/client/netmonitor/src/reducers/requests.js
@@ -23,17 +23,17 @@ const {
 /**
  * This structure stores list of all HTTP requests received
  * from the backend. It's using plain JS structures to store
  * data instead of ImmutableJS, which is performance expensive.
  */
 function Requests() {
   return {
     // Map with all requests (key = actor ID, value = request object)
-    requests: mapNew(),
+    requests: new Map(),
     // Selected request ID
     selectedId: null,
     preselectedId: null,
     // True if the monitor is recording HTTP traffic
     recording: true,
     // Auxiliary fields to hold requests stats
     firstStartedMillis: +Infinity,
     lastEndedMillis: -Infinity,
@@ -169,17 +169,17 @@ function requestsReducer(state = Request
     // Side bar with request details opened.
     case OPEN_NETWORK_DETAILS: {
       let nextState = { ...state };
       if (!action.open) {
         nextState.selectedId = null;
         return nextState;
       }
 
-      if (!state.selectedId && !state.requests.isEmpty()) {
+      if (!state.selectedId && state.requests.size > 0) {
         nextState.selectedId = [...state.requests.values()][0].id;
         return nextState;
       }
 
       return state;
     }
 
     default:
@@ -208,42 +208,29 @@ function closeCustomRequest(state) {
 
   return {
     ...state,
     requests: mapDelete(state.requests, selectedId),
     selectedId: null,
   };
 }
 
-// Immutability helpers
-// FIXME The following helper API need refactoring, see bug 1418969.
-
-/**
- * Clone an existing map.
- */
-function mapNew(map) {
-  let newMap = new Map(map);
-  newMap.isEmpty = () => newMap.size == 0;
-  newMap.valueSeq = () => [...newMap.values()];
-  return newMap;
-}
-
 /**
  * Append new item into existing map and return new map.
  */
 function mapSet(map, key, value) {
-  let newMap = mapNew(map);
+  let newMap = new Map(map);
   return newMap.set(key, value);
 }
 
 /**
  * Remove an item from existing map and return new map.
  */
 function mapDelete(map, key) {
-  let newMap = mapNew(map);
+  let newMap = new Map(map);
   newMap.delete(key);
   return newMap;
 }
 
 module.exports = {
   Requests,
   requestsReducer,
 };
--- a/devtools/client/netmonitor/src/selectors/requests.js
+++ b/devtools/client/netmonitor/src/selectors/requests.js
@@ -63,41 +63,41 @@ const getSortFn = createSelector(
     return (a, b) => ascending * sortWithClones(requests, sorter, a, b);
   }
 );
 
 const getSortedRequests = createSelector(
   state => state.requests,
   getSortFn,
   ({ requests }, sortFn) => {
-    let arr = requests.valueSeq().sort(sortFn);
+    let arr = [...requests.values()].sort(sortFn);
     arr.get = index => arr[index];
     arr.isEmpty = () => this.length == 0;
     arr.size = arr.length;
     return arr;
   }
 );
 
 const getDisplayedRequests = createSelector(
   state => state.requests,
   getFilterFn,
   getSortFn,
   ({ requests }, filterFn, sortFn) => {
-    let arr = requests.valueSeq().filter(filterFn).sort(sortFn);
+    let arr = [...requests.values()].filter(filterFn).sort(sortFn);
     arr.get = index => arr[index];
     arr.isEmpty = () => this.length == 0;
     arr.size = arr.length;
     return arr;
   }
 );
 
 const getTypeFilteredRequests = createSelector(
   state => state.requests,
   getTypeFilterFn,
-  ({ requests }, filterFn) => requests.valueSeq().filter(filterFn)
+  ({ requests }, filterFn) => [...requests.values()].filter(filterFn)
 );
 
 const getDisplayedRequestsSummary = createSelector(
   getDisplayedRequests,
   state => state.requests.lastEndedMillis - state.requests.firstStartedMillis,
   (requests, totalMillis) => {
     if (requests.size == 0) {
       return { count: 0, bytes: 0, millis: 0 };