summaryrefslogtreecommitdiffstats
path: root/devtools/client/performance/components/WaterfallTree.js
blob: c235f90b331e088a12830083516b713f864427a5 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* 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 {
  Component,
  createFactory,
} = require("devtools/client/shared/vendor/react");
const PropTypes = require("devtools/client/shared/vendor/react-prop-types");
const Tree = createFactory(
  require("devtools/client/shared/components/VirtualizedTree")
);
const WaterfallTreeRow = createFactory(
  require("devtools/client/performance/components/WaterfallTreeRow")
);

// Keep in sync with var(--waterfall-tree-row-height) in performance.css
const WATERFALL_TREE_ROW_HEIGHT = 15; // px

/**
 * Checks if a given marker is in the specified time range.
 *
 * @param object e
 *        The marker containing the { start, end } timestamps.
 * @param number start
 *        The earliest allowed time.
 * @param number end
 *        The latest allowed time.
 * @return boolean
 *         True if the marker fits inside the specified time range.
 */
function isMarkerInRange(e, start, end) {
  const mStart = e.start | 0;
  const mEnd = e.end | 0;

  return (
    // bounds inside
    (mStart >= start && mEnd <= end) ||
    // bounds outside
    (mStart < start && mEnd > end) ||
    // overlap start
    (mStart < start && mEnd >= start && mEnd <= end) ||
    // overlap end
    (mEnd > end && mStart >= start && mStart <= end)
  );
}

class WaterfallTree extends Component {
  static get propTypes() {
    return {
      marker: PropTypes.object.isRequired,
      startTime: PropTypes.number.isRequired,
      endTime: PropTypes.number.isRequired,
      dataScale: PropTypes.number.isRequired,
      sidebarWidth: PropTypes.number.isRequired,
      waterfallWidth: PropTypes.number.isRequired,
      onFocus: PropTypes.func,
    };
  }

  constructor(props) {
    super(props);

    this.state = {
      focused: null,
      expanded: new Set(),
    };

    this._getRoots = this._getRoots.bind(this);
    this._getParent = this._getParent.bind(this);
    this._getChildren = this._getChildren.bind(this);
    this._getKey = this._getKey.bind(this);
    this._isExpanded = this._isExpanded.bind(this);
    this._onExpand = this._onExpand.bind(this);
    this._onCollapse = this._onCollapse.bind(this);
    this._onFocus = this._onFocus.bind(this);
    this._filter = this._filter.bind(this);
    this._renderItem = this._renderItem.bind(this);
  }

  _getRoots(node) {
    const roots = this.props.marker.submarkers || [];
    return roots.filter(this._filter);
  }

  /**
   * Find the parent node of 'node' with a depth-first search of the marker tree
   */
  _getParent(node) {
    function findParent(marker) {
      if (marker.submarkers) {
        for (const submarker of marker.submarkers) {
          if (submarker === node) {
            return marker;
          }

          const parent = findParent(submarker);
          if (parent) {
            return parent;
          }
        }
      }

      return null;
    }

    const rootMarker = this.props.marker;
    const parent = findParent(rootMarker);

    // We are interested only in parent markers that are rendered,
    // which rootMarker is not. Return null if the parent is rootMarker.
    return parent !== rootMarker ? parent : null;
  }

  _getChildren(node) {
    const submarkers = node.submarkers || [];
    return submarkers.filter(this._filter);
  }

  _getKey(node) {
    return `marker-${node.index}`;
  }

  _isExpanded(node) {
    return this.state.expanded.has(node);
  }

  _onExpand(node) {
    this.setState(state => {
      const expanded = new Set(state.expanded);
      expanded.add(node);
      return { expanded };
    });
  }

  _onCollapse(node) {
    this.setState(state => {
      const expanded = new Set(state.expanded);
      expanded.delete(node);
      return { expanded };
    });
  }

  _onFocus(node) {
    this.setState({ focused: node });
    if (this.props.onFocus) {
      this.props.onFocus(node);
    }
  }

  _filter(node) {
    const { startTime, endTime } = this.props;
    return isMarkerInRange(node, startTime, endTime);
  }

  _renderItem(marker, level, focused, arrow, expanded) {
    const { startTime, dataScale, sidebarWidth } = this.props;
    return WaterfallTreeRow({
      marker,
      level,
      arrow,
      expanded,
      focused,
      startTime,
      dataScale,
      sidebarWidth,
    });
  }

  render() {
    return Tree({
      preventNavigationOnArrowRight: false,
      getRoots: this._getRoots,
      getParent: this._getParent,
      getChildren: this._getChildren,
      getKey: this._getKey,
      isExpanded: this._isExpanded,
      onExpand: this._onExpand,
      onCollapse: this._onCollapse,
      onFocus: this._onFocus,
      renderItem: this._renderItem,
      focused: this.state.focused,
      itemHeight: WATERFALL_TREE_ROW_HEIGHT,
    });
  }
}

module.exports = WaterfallTree;