summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/redux/middleware/performance-marker.js
blob: c829dffe3e7e9e2faf7f03fd75db110c57580819 (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
/* 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";

/**
 * This function returns a middleware, which is responsible for adding markers that will
 * be visible in performance profiles, and may help investigate performance issues.
 *
 * Example usage, adding a marker when console messages are added, and when they are cleared:
 *
 * return createPerformanceMarkerMiddleware({
 *   "MESSAGES_ADD": {
 *     label: "WebconsoleAddMessages",
 *     sessionId: 12345,
 *     getMarkerDescription: function({ action, state }) {
 *       const { messages } = action;
 *       const totalMessageCount = state.messages.mutableMessagesById.size;
 *       return `${messages.length} messages handled, store now has ${totalMessageCount} messages`;
 *     },
 *   },
 *   "MESSAGES_CLEARED": {
 *     label: "WebconsoleClearMessages",
 *     sessionId: 12345
 *   },
 * });
 *
 * @param {Object} cases: An object, keyed by action type, that will determine if a
 *         given action will add a marker.
 * @param {String} cases.{actionType} - The type of the action that will trigger the
 *         marker creation.
 * @param {String} cases.{actionType}.label - The marker label
 * @param {Integer} cases.{actionType}.sessionId - The telemetry sessionId. This is used
 *        to be able to distinguish markers coming from different toolboxes.
 * @param {Function} [cases.{actionType}.getMarkerDescription] - An optional function that
 *        will be called when adding the marker to populate its description. The function
 *        is called with an object holding the action and the state
 */
function createPerformanceMarkerMiddleware(cases) {
  return function (store) {
    return next => action => {
      const condition = cases[action.type];
      const shouldAddProfileMarker = !!condition;

      // Start the marker timer before calling next(action).
      const startTime = shouldAddProfileMarker ? Cu.now() : null;
      const newState = next(action);

      if (shouldAddProfileMarker) {
        ChromeUtils.addProfilerMarker(
          `${condition.label} ${condition.sessionId}`,
          startTime,
          condition.getMarkerDescription
            ? condition.getMarkerDescription({
                action,
                state: store.getState(),
              })
            : ""
        );
      }
      return newState;
    };
  };
}

module.exports = {
  createPerformanceMarkerMiddleware,
};