mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-23 02:05:42 +00:00
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 --HG-- extra : moz-landing-system : lando
This commit is contained in:
parent
02e7bc79d9
commit
5d2f689907
@ -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: {
|
||||
|
Loading…
Reference in New Issue
Block a user