tor-browser

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

ProfilerEventHandling.js (3665B)


      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
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 // @ts-check
      5 
      6 /**
      7 * @typedef {import("../../@types/perf").PerfFront} PerfFront
      8 * @typedef {import("../../@types/perf").RecordingState} RecordingState
      9 * @typedef {import("../../@types/perf").State} StoreState
     10 * @typedef {import("../../@types/perf").RootTraits} RootTraits
     11 * @typedef {import("../../@types/perf").PanelWindow} PanelWindow
     12 */
     13 
     14 /**
     15 * @template P
     16 * @typedef {import("react-redux").ResolveThunks<P>} ResolveThunks<P>
     17 */
     18 
     19 /**
     20 * @typedef {object} StateProps
     21 * @property {RecordingState} recordingState
     22 * @property {boolean?} isSupportedPlatform
     23 */
     24 
     25 /**
     26 * @typedef {object} ThunkDispatchProps
     27 * @property {typeof actions.reportProfilerReady} reportProfilerReady
     28 * @property {typeof actions.reportProfilerStarted} reportProfilerStarted
     29 * @property {typeof actions.reportProfilerStopped} reportProfilerStopped
     30 */
     31 
     32 /**
     33 * @typedef {object} OwnProps
     34 * @property {PerfFront} perfFront
     35 * @property {RootTraits} traits
     36 */
     37 
     38 /**
     39 * @typedef {ResolveThunks<ThunkDispatchProps>} DispatchProps
     40 * @typedef {StateProps & DispatchProps & OwnProps} Props
     41 */
     42 
     43 "use strict";
     44 
     45 const {
     46  PureComponent,
     47 } = require("resource://devtools/client/shared/vendor/react.mjs");
     48 const {
     49  connect,
     50 } = require("resource://devtools/client/shared/vendor/react-redux.js");
     51 const actions = require("resource://devtools/client/performance-new/store/actions.js");
     52 const selectors = require("resource://devtools/client/performance-new/store/selectors.js");
     53 
     54 /**
     55 * This component state changes for the performance recording. e.g. If the profiler
     56 * suddenly becomes unavailable, it needs to react to those changes, and update the
     57 * recordingState in the store.
     58 *
     59 * @augments {React.PureComponent<Props>}
     60 */
     61 class ProfilerEventHandling extends PureComponent {
     62  componentDidMount() {
     63    const {
     64      perfFront,
     65      isSupportedPlatform,
     66      reportProfilerReady,
     67      reportProfilerStarted,
     68      reportProfilerStopped,
     69    } = this.props;
     70 
     71    if (!isSupportedPlatform) {
     72      return;
     73    }
     74 
     75    // Ask for the initial state of the profiler.
     76    perfFront.isActive().then(isActive => reportProfilerReady(isActive));
     77 
     78    // Handle when the profiler changes state. It might be us, it might be someone else.
     79    this.props.perfFront.on("profiler-started", reportProfilerStarted);
     80    this.props.perfFront.on("profiler-stopped", reportProfilerStopped);
     81  }
     82 
     83  componentWillUnmount() {
     84    switch (this.props.recordingState) {
     85      case "not-yet-known":
     86      case "available-to-record":
     87      case "request-to-stop-profiler":
     88      case "request-to-get-profile-and-stop-profiler":
     89        // Do nothing for these states.
     90        break;
     91 
     92      case "recording":
     93      case "request-to-start-recording":
     94        this.props.perfFront.stopProfilerAndDiscardProfile();
     95        break;
     96 
     97      default:
     98        throw new Error("Unhandled recording state.");
     99    }
    100  }
    101 
    102  render() {
    103    return null;
    104  }
    105 }
    106 
    107 /**
    108 * @param {StoreState} state
    109 * @returns {StateProps}
    110 */
    111 function mapStateToProps(state) {
    112  return {
    113    recordingState: selectors.getRecordingState(state),
    114    isSupportedPlatform: selectors.getIsSupportedPlatform(state),
    115  };
    116 }
    117 
    118 /** @type {ThunkDispatchProps} */
    119 const mapDispatchToProps = {
    120  reportProfilerReady: actions.reportProfilerReady,
    121  reportProfilerStarted: actions.reportProfilerStarted,
    122  reportProfilerStopped: actions.reportProfilerStopped,
    123 };
    124 
    125 module.exports = connect(
    126  mapStateToProps,
    127  mapDispatchToProps
    128 )(ProfilerEventHandling);