tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

initializer.js (2496B)


      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 
      5 /* exported initialize, destroy, Promise */
      6 
      7 "use strict";
      8 
      9 const {
     10  createFactory,
     11  createElement,
     12 } = require("resource://devtools/client/shared/vendor/react.mjs");
     13 const ReactDOM = require("resource://devtools/client/shared/vendor/react-dom.mjs");
     14 const {
     15  Provider,
     16 } = require("resource://devtools/client/shared/vendor/react-redux.js");
     17 const App = createFactory(require("resource://devtools/client/memory/app.js"));
     18 const Store = require("resource://devtools/client/memory/store.js");
     19 const { assert } = require("resource://devtools/shared/DevToolsUtils.js");
     20 const {
     21  START_IGNORE_ACTION,
     22 } = require("resource://devtools/client/shared/redux/middleware/ignore.js");
     23 
     24 const {
     25  updateMemoryFront,
     26 } = require("resource://devtools/client/memory/actions/front.js");
     27 
     28 // Shared variables used by several methods of this module.
     29 let root, store, unsubscribe;
     30 
     31 const initialize = async function (commands) {
     32  // Exposed by panel.js
     33  const { gToolbox, gHeapAnalysesClient } = window;
     34 
     35  root = document.querySelector("#app");
     36  store = Store();
     37  const app = createElement(App, {
     38    toolbox: gToolbox,
     39    commands,
     40    heapWorker: gHeapAnalysesClient,
     41  });
     42  const provider = createElement(Provider, { store }, app);
     43  ReactDOM.render(provider, root);
     44  unsubscribe = store.subscribe(onStateChange);
     45 
     46  // Exposed for tests.
     47  window.gStore = store;
     48 };
     49 
     50 const updateFront = front => {
     51  store.dispatch(updateMemoryFront(front));
     52 };
     53 
     54 const destroy = function () {
     55  // Prevents any further action from being dispatched
     56  store.dispatch(START_IGNORE_ACTION);
     57 
     58  const ok = ReactDOM.unmountComponentAtNode(root);
     59  assert(
     60    ok,
     61    "Should successfully unmount the memory tool's top level React component"
     62  );
     63 
     64  unsubscribe();
     65 };
     66 
     67 // Current state
     68 let isHighlighted;
     69 
     70 /**
     71 * Fired on any state change, currently only handles toggling
     72 * the highlighting of the tool when recording allocations.
     73 */
     74 function onStateChange() {
     75  const { gToolbox } = window;
     76 
     77  const isRecording = store.getState().allocations.recording;
     78  if (isRecording === isHighlighted) {
     79    return;
     80  }
     81 
     82  if (isRecording) {
     83    gToolbox.highlightTool("memory");
     84  } else {
     85    gToolbox.unhighlightTool("memory");
     86  }
     87 
     88  isHighlighted = isRecording;
     89 }
     90 
     91 module.exports = { initialize, updateFront, destroy };