individuals.js (2353B)
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 file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 "use strict"; 5 6 const { 7 assert, 8 immutableUpdate, 9 } = require("resource://devtools/shared/DevToolsUtils.js"); 10 const { 11 actions, 12 individualsState, 13 viewState, 14 } = require("resource://devtools/client/memory/constants.js"); 15 16 const handlers = Object.create(null); 17 18 handlers[actions.POP_VIEW] = function (_state, _action) { 19 return null; 20 }; 21 22 handlers[actions.CHANGE_VIEW] = function (individuals, { newViewState }) { 23 if (newViewState === viewState.INDIVIDUALS) { 24 assert( 25 !individuals, 26 "Should not switch to individuals view when already in individuals view" 27 ); 28 return Object.freeze({ 29 state: individualsState.COMPUTING_DOMINATOR_TREE, 30 }); 31 } 32 33 return null; 34 }; 35 36 handlers[actions.FOCUS_INDIVIDUAL] = function (individuals, { node }) { 37 assert(individuals, "Should have individuals"); 38 return immutableUpdate(individuals, { focused: node }); 39 }; 40 41 handlers[actions.FETCH_INDIVIDUALS_START] = function (individuals) { 42 assert(individuals, "Should have individuals"); 43 return Object.freeze({ 44 state: individualsState.FETCHING, 45 focused: individuals.focused, 46 }); 47 }; 48 49 handlers[actions.FETCH_INDIVIDUALS_END] = function (individuals, action) { 50 assert(individuals, "Should have individuals"); 51 assert(!individuals.nodes, "Should not have nodes"); 52 assert( 53 individuals.state === individualsState.FETCHING, 54 "Should only end fetching individuals after starting." 55 ); 56 57 const focused = individuals.focused 58 ? action.nodes.find(n => n.nodeId === individuals.focused.nodeId) 59 : null; 60 61 return Object.freeze({ 62 state: individualsState.FETCHED, 63 nodes: action.nodes, 64 id: action.id, 65 censusBreakdown: action.censusBreakdown, 66 indices: action.indices, 67 labelDisplay: action.labelDisplay, 68 focused, 69 dominatorTree: action.dominatorTree, 70 }); 71 }; 72 73 handlers[actions.INDIVIDUALS_ERROR] = function (_, { error }) { 74 return Object.freeze({ 75 error, 76 nodes: null, 77 state: individualsState.ERROR, 78 }); 79 }; 80 81 module.exports = function (individuals = null, action) { 82 const handler = handlers[action.type]; 83 return handler ? handler(individuals, action) : individuals; 84 };