summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/components/reps/reps/promise.js
blob: db21f294779995e996ea9c34476f0edc025d81ed (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
/* 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";

// Make this available to both AMD and CJS environments
define(function (require, exports, module) {
  // ReactJS
  const PropTypes = require("devtools/client/shared/vendor/react-prop-types");
  const { span } = require("devtools/client/shared/vendor/react-dom-factories");

  // Dependencies
  const {
    getGripType,
    wrapRender,
  } = require("devtools/client/shared/components/reps/reps/rep-utils");

  const Grip = require("devtools/client/shared/components/reps/reps/grip");
  const {
    MODE,
  } = require("devtools/client/shared/components/reps/reps/constants");

  /**
   * Renders a DOM Promise object.
   */

  PromiseRep.propTypes = {
    object: PropTypes.object.isRequired,
    mode: PropTypes.oneOf(Object.values(MODE)),
    onDOMNodeMouseOver: PropTypes.func,
    onDOMNodeMouseOut: PropTypes.func,
    onInspectIconClick: PropTypes.func,
    shouldRenderTooltip: PropTypes.bool,
  };

  function PromiseRep(props) {
    const object = props.object;

    // @backward-compat { version 85 } On older servers, the preview of a promise was
    // useless and didn't include the internal promise state, which was directly exposed
    // in the grip.
    if (object.promiseState) {
      const { state, value, reason } = object.promiseState;
      const ownProperties = Object.create(null);
      ownProperties["<state>"] = { value: state };
      let ownPropertiesLength = 1;
      if (state == "fulfilled") {
        ownProperties["<value>"] = { value };
        ++ownPropertiesLength;
      } else if (state == "rejected") {
        ownProperties["<reason>"] = { value: reason };
        ++ownPropertiesLength;
      }
      object.preview = {
        kind: "Object",
        ownProperties,
        ownPropertiesLength,
      };
    }

    if (props.mode !== MODE.TINY) {
      return Grip.rep(props);
    }

    const shouldRenderTooltip = props.shouldRenderTooltip;
    const config = {
      "data-link-actor-id": object.actor,
      className: "objectBox objectBox-object",
      title: shouldRenderTooltip ? "Promise" : null,
    };

    const { Rep } = require("devtools/client/shared/components/reps/reps/rep");

    return span(
      config,
      getTitle(object),
      span({ className: "objectLeftBrace" }, " { "),
      Rep({ object: object.preview.ownProperties["<state>"].value }),
      span({ className: "objectRightBrace" }, " }")
    );
  }

  function getTitle(object) {
    return span({ className: "objectTitle" }, object.class);
  }

  // Registration
  function supportsObject(object, noGrip = false) {
    if (!Grip.supportsObject(object, noGrip)) {
      return false;
    }
    return getGripType(object, noGrip) == "Promise";
  }

  // Exports from this module
  module.exports = {
    rep: wrapRender(PromiseRep),
    supportsObject,
  };
});