summaryrefslogtreecommitdiffstats
path: root/devtools/client/memory/reducers/view.js
blob: 5d31e0c9b52dc458c5c8d4ecb03d3966842ad6ab (plain)
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
/* 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 { assert } = require("devtools/shared/DevToolsUtils");
const { actions, viewState } = require("../constants");

const handlers = Object.create(null);

handlers[actions.POP_VIEW] = function (view, _) {
  assert(view.previous, "Had better have a previous view state when POP_VIEW");
  return Object.freeze({
    state: view.previous.state,
    previous: null,
  });
};

handlers[actions.CHANGE_VIEW] = function (view, action) {
  const { newViewState, oldDiffing, oldSelected } = action;
  assert(newViewState);

  if (newViewState === viewState.INDIVIDUALS) {
    assert(oldDiffing || oldSelected);
    return Object.freeze({
      state: newViewState,
      previous: Object.freeze({
        state: view.state,
        selected: oldSelected,
        diffing: oldDiffing,
      }),
    });
  }

  return Object.freeze({
    state: newViewState,
    previous: null,
  });
};

const DEFAULT_VIEW = {
  state: viewState.TREE_MAP,
  previous: null,
};

module.exports = function (view = DEFAULT_VIEW, action) {
  const handler = handlers[action.type];
  return handler ? handler(view, action) : view;
};