view.js (1378B)
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 { assert } = require("resource://devtools/shared/DevToolsUtils.js"); 7 const { 8 actions, 9 viewState, 10 } = require("resource://devtools/client/memory/constants.js"); 11 12 const handlers = Object.create(null); 13 14 handlers[actions.POP_VIEW] = function (view, _) { 15 assert(view.previous, "Had better have a previous view state when POP_VIEW"); 16 return Object.freeze({ 17 state: view.previous.state, 18 previous: null, 19 }); 20 }; 21 22 handlers[actions.CHANGE_VIEW] = function (view, action) { 23 const { newViewState, oldDiffing, oldSelected } = action; 24 assert(newViewState); 25 26 if (newViewState === viewState.INDIVIDUALS) { 27 assert(oldDiffing || oldSelected); 28 return Object.freeze({ 29 state: newViewState, 30 previous: Object.freeze({ 31 state: view.state, 32 selected: oldSelected, 33 diffing: oldDiffing, 34 }), 35 }); 36 } 37 38 return Object.freeze({ 39 state: newViewState, 40 previous: null, 41 }); 42 }; 43 44 const DEFAULT_VIEW = { 45 state: viewState.TREE_MAP, 46 previous: null, 47 }; 48 49 module.exports = function (view = DEFAULT_VIEW, action) { 50 const handler = handlers[action.type]; 51 return handler ? handler(view, action) : view; 52 };