runtimes-state.js (5477B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { 8 CONNECT_RUNTIME_CANCEL, 9 CONNECT_RUNTIME_FAILURE, 10 CONNECT_RUNTIME_NOT_RESPONDING, 11 CONNECT_RUNTIME_START, 12 CONNECT_RUNTIME_SUCCESS, 13 DISCONNECT_RUNTIME_SUCCESS, 14 RUNTIMES, 15 UPDATE_CONNECTION_PROMPT_SETTING_SUCCESS, 16 REMOTE_RUNTIMES_UPDATED, 17 SELECTED_RUNTIME_ID_UPDATED, 18 THIS_FIREFOX_RUNTIME_CREATED, 19 } = require("resource://devtools/client/aboutdebugging/src/constants.js"); 20 21 const { 22 findRuntimeById, 23 } = require("resource://devtools/client/aboutdebugging/src/modules/runtimes-state-helper.js"); 24 25 const { 26 remoteClientManager, 27 } = require("resource://devtools/client/shared/remote-debugging/remote-client-manager.js"); 28 29 // Map between known runtime types and nodes in the runtimes state. 30 const TYPE_TO_RUNTIMES_KEY = { 31 [RUNTIMES.THIS_FIREFOX]: "thisFirefoxRuntimes", 32 [RUNTIMES.NETWORK]: "networkRuntimes", 33 [RUNTIMES.USB]: "usbRuntimes", 34 }; 35 36 function RuntimesState() { 37 return { 38 networkRuntimes: [], 39 selectedRuntimeId: null, 40 // "This Firefox" runtimes is an array for consistency, but it should only contain one 41 // runtime. This runtime will be added after initializing the application via 42 // THIS_FIREFOX_RUNTIME_CREATED. 43 thisFirefoxRuntimes: [], 44 usbRuntimes: [], 45 }; 46 } 47 48 /** 49 * Update the runtime matching the provided runtimeId with the content of updatedRuntime, 50 * and return the new state. 51 * 52 * @param {string} runtimeId 53 * The id of the runtime to update 54 * @param {object} updatedRuntime 55 * Object used to update the runtime matching the idea using Object.assign. 56 * @param {object} state 57 * Current runtimes state. 58 * @return {object} The updated state 59 */ 60 function _updateRuntimeById(runtimeId, updatedRuntime, state) { 61 // Find the array of runtimes that contains the updated runtime. 62 const runtime = findRuntimeById(runtimeId, state); 63 const key = TYPE_TO_RUNTIMES_KEY[runtime.type]; 64 const runtimesToUpdate = state[key]; 65 66 // Update the runtime with the provided updatedRuntime. 67 const updatedRuntimes = runtimesToUpdate.map(r => { 68 if (r.id === runtimeId) { 69 return Object.assign({}, r, updatedRuntime); 70 } 71 return r; 72 }); 73 return Object.assign({}, state, { [key]: updatedRuntimes }); 74 } 75 76 function runtimesReducer(state = RuntimesState(), action) { 77 switch (action.type) { 78 case CONNECT_RUNTIME_START: { 79 const { id } = action; 80 const updatedState = { 81 isConnecting: true, 82 isConnectionFailed: false, 83 isConnectionNotResponding: false, 84 isConnectionTimeout: false, 85 }; 86 return _updateRuntimeById(id, updatedState, state); 87 } 88 89 case CONNECT_RUNTIME_NOT_RESPONDING: { 90 const { id } = action; 91 return _updateRuntimeById(id, { isConnectionNotResponding: true }, state); 92 } 93 94 case CONNECT_RUNTIME_CANCEL: { 95 const { id } = action; 96 const updatedState = { 97 isConnecting: false, 98 isConnectionFailed: false, 99 isConnectionNotResponding: false, 100 isConnectionTimeout: true, 101 }; 102 return _updateRuntimeById(id, updatedState, state); 103 } 104 105 case CONNECT_RUNTIME_SUCCESS: { 106 const { id, runtimeDetails, type } = action.runtime; 107 108 // Update the remoteClientManager with the connected runtime. 109 const client = runtimeDetails.clientWrapper.client; 110 const runtimeInfo = runtimeDetails.info; 111 remoteClientManager.setClient(id, type, client, runtimeInfo); 112 113 const updatedState = { 114 isConnecting: false, 115 isConnectionFailed: false, 116 isConnectionNotResponding: false, 117 isConnectionTimeout: false, 118 runtimeDetails, 119 }; 120 return _updateRuntimeById(id, updatedState, state); 121 } 122 123 case CONNECT_RUNTIME_FAILURE: { 124 const { id } = action; 125 const updatedState = { 126 isConnecting: false, 127 isConnectionFailed: true, 128 isConnectionNotResponding: false, 129 isConnectionTimeout: false, 130 }; 131 return _updateRuntimeById(id, updatedState, state); 132 } 133 134 case DISCONNECT_RUNTIME_SUCCESS: { 135 const { id, type } = action.runtime; 136 remoteClientManager.removeClient(id, type); 137 return _updateRuntimeById(id, { runtimeDetails: null }, state); 138 } 139 140 case SELECTED_RUNTIME_ID_UPDATED: { 141 const selectedRuntimeId = action.runtimeId || null; 142 return Object.assign({}, state, { selectedRuntimeId }); 143 } 144 145 case UPDATE_CONNECTION_PROMPT_SETTING_SUCCESS: { 146 const { connectionPromptEnabled } = action; 147 const { id: runtimeId } = action.runtime; 148 const runtime = findRuntimeById(runtimeId, state); 149 const runtimeDetails = Object.assign({}, runtime.runtimeDetails, { 150 connectionPromptEnabled, 151 }); 152 return _updateRuntimeById(runtimeId, { runtimeDetails }, state); 153 } 154 155 case REMOTE_RUNTIMES_UPDATED: { 156 const { runtimes, runtimeType } = action; 157 const key = TYPE_TO_RUNTIMES_KEY[runtimeType]; 158 return Object.assign({}, state, { 159 [key]: runtimes, 160 }); 161 } 162 163 case THIS_FIREFOX_RUNTIME_CREATED: { 164 const { runtime } = action; 165 return Object.assign({}, state, { 166 thisFirefoxRuntimes: [runtime], 167 }); 168 } 169 170 default: 171 return state; 172 } 173 } 174 175 module.exports = { 176 RuntimesState, 177 runtimesReducer, 178 };