diff --git a/devtools/client/aboutdebugging-new/src/reducers/runtimes-state.js b/devtools/client/aboutdebugging-new/src/reducers/runtimes-state.js index 2d2cd0d7a3af..66363f4ea186 100644 --- a/devtools/client/aboutdebugging-new/src/reducers/runtimes-state.js +++ b/devtools/client/aboutdebugging-new/src/reducers/runtimes-state.js @@ -37,42 +37,44 @@ function RuntimesState() { }; } +/** + * 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: {