summaryrefslogtreecommitdiffstats
path: root/devtools/client/memory/initializer.js
blob: 541bd224eb3d7e5cd56836515a2c6f5b7f4fa7a5 (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
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
/* 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/. */

/* exported initialize, destroy, Promise */

"use strict";

const {
  createFactory,
  createElement,
} = require("resource://devtools/client/shared/vendor/react.js");
const ReactDOM = require("resource://devtools/client/shared/vendor/react-dom.js");
const {
  Provider,
} = require("resource://devtools/client/shared/vendor/react-redux.js");
const App = createFactory(require("resource://devtools/client/memory/app.js"));
const Store = require("resource://devtools/client/memory/store.js");
const { assert } = require("resource://devtools/shared/DevToolsUtils.js");

const {
  updateMemoryFront,
} = require("resource://devtools/client/memory/actions/front.js");

// Shared variables used by several methods of this module.
let root, store, unsubscribe;

const initialize = async function (commands) {
  // Exposed by panel.js
  const { gToolbox, gHeapAnalysesClient } = window;

  root = document.querySelector("#app");
  store = Store();
  const app = createElement(App, {
    toolbox: gToolbox,
    commands,
    heapWorker: gHeapAnalysesClient,
  });
  const provider = createElement(Provider, { store }, app);
  ReactDOM.render(provider, root);
  unsubscribe = store.subscribe(onStateChange);

  // Exposed for tests.
  window.gStore = store;
};

const updateFront = front => {
  store.dispatch(updateMemoryFront(front));
};

const destroy = function () {
  const ok = ReactDOM.unmountComponentAtNode(root);
  assert(
    ok,
    "Should successfully unmount the memory tool's top level React component"
  );

  unsubscribe();
};

// Current state
let isHighlighted;

/**
 * Fired on any state change, currently only handles toggling
 * the highlighting of the tool when recording allocations.
 */
function onStateChange() {
  const { gToolbox } = window;

  const isRecording = store.getState().allocations.recording;
  if (isRecording === isHighlighted) {
    return;
  }

  if (isRecording) {
    gToolbox.highlightTool("memory");
  } else {
    gToolbox.unhighlightTool("memory");
  }

  isHighlighted = isRecording;
}

module.exports = { initialize, updateFront, destroy };