Bug 1491764 - Mutualize runtimes reducer logic to update single runtime;r=ladybenko
authorJulian Descottes <jdescottes@mozilla.com>
Wed, 03 Oct 2018 10:33:48 +0000
changeset 487953 6a2f74caa1c2afa5bc7af596ca37880939aaaa30
parent 487952 594454d6b1cce40d69e021d5cf6e5aada279a38d
child 487954 e28a0bc4a7c53349e8490f41287c9e027037567f
push id246
push userfmarier@mozilla.com
push dateSat, 13 Oct 2018 00:15:40 +0000
reviewersladybenko
bugs1491764
milestone64.0a1
Bug 1491764 - Mutualize runtimes reducer logic to update single runtime;r=ladybenko Depends on D7308 . This changeset introduces a common helper to modify a single runtime in the list of runtimes. Differential Revision: https://phabricator.services.mozilla.com/D7309
devtools/client/aboutdebugging-new/src/reducers/runtimes-state.js
--- a/devtools/client/aboutdebugging-new/src/reducers/runtimes-state.js
+++ b/devtools/client/aboutdebugging-new/src/reducers/runtimes-state.js
@@ -32,52 +32,54 @@ function RuntimesState() {
     thisFirefoxRuntimes: [{
       id: RUNTIMES.THIS_FIREFOX,
       type: RUNTIMES.THIS_FIREFOX,
     }],
     usbRuntimes: [],
   };
 }
 
+/**
+ * Update the runtime matching the provided runtimeId with the content of updatedRuntime,
+ * and return the new state.
+ *
+ * @param  {String} runtimeId
+ *         The id of the runtime to update
+ * @param  {Object} updatedRuntime
+ *         Object used to update the runtime matching the idea using Object.assign.
+ * @param  {Object} state
+ *         Current runtimes state.
+ * @return {Object} The updated state
+ */
+function _updateRuntimeById(runtimeId, updatedRuntime, state) {
+  // Find the array of runtimes that contains the updated runtime.
+  const runtime = findRuntimeById(runtimeId, state);
+  const key = TYPE_TO_RUNTIMES_KEY[runtime.type];
+  const runtimesToUpdate = state[key];
+
+  // Update the runtime with the provided updatedRuntime.
+  const updatedRuntimes = runtimesToUpdate.map(r => {
+    if (r.id === runtimeId) {
+      return Object.assign({}, r, updatedRuntime);
+    }
+    return r;
+  });
+  return Object.assign({}, state, { [key]: updatedRuntimes });
+}
+
 function runtimesReducer(state = RuntimesState(), action) {
   switch (action.type) {
     case CONNECT_RUNTIME_SUCCESS: {
       const { id, client } = action.runtime;
-
-      // Find the array of runtimes that contains the updated runtime.
-      const runtime = findRuntimeById(id, state);
-      const key = TYPE_TO_RUNTIMES_KEY[runtime.type];
-      const runtimesToUpdate = state[key];
-
-      // Add the new client to the runtime.
-      const updatedRuntimes = runtimesToUpdate.map(r => {
-        if (r.id === id) {
-          return Object.assign({}, r, { client });
-        }
-        return r;
-      });
-      return Object.assign({}, state, { [key]: updatedRuntimes });
+      return _updateRuntimeById(id, { client }, state);
     }
 
     case DISCONNECT_RUNTIME_SUCCESS: {
       const { id } = action.runtime;
-
-      // Find the array of runtimes that contains the updated runtime.
-      const runtime = findRuntimeById(id, state);
-      const key = TYPE_TO_RUNTIMES_KEY[runtime.type];
-      const runtimesToUpdate = state[key];
-
-      // Remove the client from the updated runtime.
-      const updatedRuntimes = runtimesToUpdate.map(r => {
-        if (r.id === id) {
-          return Object.assign({}, r, { client: null });
-        }
-        return r;
-      });
-      return Object.assign({}, state, { [key]: updatedRuntimes });
+      return _updateRuntimeById(id, { client: null }, state);
     }
 
     case NETWORK_LOCATIONS_UPDATED: {
       const { locations } = action;
       const networkRuntimes = locations.map(location => {
         return {
           id: location,
           name: location,