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