promise.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. "use strict";
  6. // Make this available to both AMD and CJS environments
  7. define(function (require, exports, module) {
  8. // ReactJS
  9. const React = require("devtools/client/shared/vendor/react");
  10. // Dependencies
  11. const { createFactories, isGrip } = require("./rep-utils");
  12. const { PropRep } = createFactories(require("./prop-rep"));
  13. // Shortcuts
  14. const { span } = React.DOM;
  15. /**
  16. * Renders a DOM Promise object.
  17. */
  18. const PromiseRep = React.createClass({
  19. displayName: "Promise",
  20. propTypes: {
  21. object: React.PropTypes.object.isRequired,
  22. mode: React.PropTypes.string,
  23. },
  24. getTitle: function (object) {
  25. const title = object.class;
  26. if (this.props.objectLink) {
  27. return this.props.objectLink({
  28. object: object
  29. }, title);
  30. }
  31. return title;
  32. },
  33. getProps: function (promiseState) {
  34. const keys = ["state"];
  35. if (Object.keys(promiseState).includes("value")) {
  36. keys.push("value");
  37. }
  38. return keys.map((key, i) => {
  39. return PropRep(Object.assign({}, this.props, {
  40. mode: "tiny",
  41. name: `<${key}>`,
  42. object: promiseState[key],
  43. equal: ": ",
  44. delim: i < keys.length - 1 ? ", " : ""
  45. }));
  46. });
  47. },
  48. render: function () {
  49. const object = this.props.object;
  50. const {promiseState} = object;
  51. let objectLink = this.props.objectLink || span;
  52. if (this.props.mode == "tiny") {
  53. let { Rep } = createFactories(require("./rep"));
  54. return (
  55. span({className: "objectBox objectBox-object"},
  56. this.getTitle(object),
  57. objectLink({
  58. className: "objectLeftBrace",
  59. object: object
  60. }, " { "),
  61. Rep({object: promiseState.state}),
  62. objectLink({
  63. className: "objectRightBrace",
  64. object: object
  65. }, " }")
  66. )
  67. );
  68. }
  69. const props = this.getProps(promiseState);
  70. return (
  71. span({className: "objectBox objectBox-object"},
  72. this.getTitle(object),
  73. objectLink({
  74. className: "objectLeftBrace",
  75. object: object
  76. }, " { "),
  77. ...props,
  78. objectLink({
  79. className: "objectRightBrace",
  80. object: object
  81. }, " }")
  82. )
  83. );
  84. },
  85. });
  86. // Registration
  87. function supportsObject(object, type) {
  88. if (!isGrip(object)) {
  89. return false;
  90. }
  91. return type === "Promise";
  92. }
  93. // Exports from this module
  94. exports.PromiseRep = {
  95. rep: PromiseRep,
  96. supportsObject: supportsObject
  97. };
  98. });