PerformanceWatchdog.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import defined from "../../Core/defined.js";
  2. import destroyObject from "../../Core/destroyObject.js";
  3. import DeveloperError from "../../Core/DeveloperError.js";
  4. import knockout from "../../ThirdParty/knockout.js";
  5. import getElement from "../getElement.js";
  6. import PerformanceWatchdogViewModel from "./PerformanceWatchdogViewModel.js";
  7. /**
  8. * Monitors performance of the application and displays a message if poor performance is detected.
  9. *
  10. * @alias PerformanceWatchdog
  11. * @constructor
  12. *
  13. * @param {Object} [options] Object with the following properties:
  14. * @param {Element|String} options.container The DOM element or ID that will contain the widget.
  15. * @param {Scene} options.scene The {@link Scene} for which to monitor performance.
  16. * @param {String} [options.lowFrameRateMessage='This application appears to be performing poorly on your system. Please try using a different web browser or updating your video drivers.'] The
  17. * message to display when a low frame rate is detected. The message is interpeted as HTML, so make sure
  18. * it comes from a trusted source so that your application is not vulnerable to cross-site scripting attacks.
  19. */
  20. function PerformanceWatchdog(options) {
  21. //>>includeStart('debug', pragmas.debug);
  22. if (!defined(options) || !defined(options.container)) {
  23. throw new DeveloperError("options.container is required.");
  24. }
  25. if (!defined(options.scene)) {
  26. throw new DeveloperError("options.scene is required.");
  27. }
  28. //>>includeEnd('debug');
  29. const container = getElement(options.container);
  30. const viewModel = new PerformanceWatchdogViewModel(options);
  31. const element = document.createElement("div");
  32. element.className = "cesium-performance-watchdog-message-area";
  33. element.setAttribute("data-bind", "visible: showingLowFrameRateMessage");
  34. const dismissButton = document.createElement("button");
  35. dismissButton.setAttribute("type", "button");
  36. dismissButton.className = "cesium-performance-watchdog-message-dismiss";
  37. dismissButton.innerHTML = "×";
  38. dismissButton.setAttribute("data-bind", "click: dismissMessage");
  39. element.appendChild(dismissButton);
  40. const message = document.createElement("div");
  41. message.className = "cesium-performance-watchdog-message";
  42. message.setAttribute("data-bind", "html: lowFrameRateMessage");
  43. element.appendChild(message);
  44. container.appendChild(element);
  45. knockout.applyBindings(viewModel, element);
  46. this._container = container;
  47. this._viewModel = viewModel;
  48. this._element = element;
  49. }
  50. Object.defineProperties(PerformanceWatchdog.prototype, {
  51. /**
  52. * Gets the parent container.
  53. * @memberof PerformanceWatchdog.prototype
  54. *
  55. * @type {Element}
  56. */
  57. container: {
  58. get: function () {
  59. return this._container;
  60. },
  61. },
  62. /**
  63. * Gets the view model.
  64. * @memberof PerformanceWatchdog.prototype
  65. *
  66. * @type {PerformanceWatchdogViewModel}
  67. */
  68. viewModel: {
  69. get: function () {
  70. return this._viewModel;
  71. },
  72. },
  73. });
  74. /**
  75. * @memberof PerformanceWatchdog
  76. * @returns {Boolean} true if the object has been destroyed, false otherwise.
  77. */
  78. PerformanceWatchdog.prototype.isDestroyed = function () {
  79. return false;
  80. };
  81. /**
  82. * Destroys the widget. Should be called if permanently
  83. * removing the widget from layout.
  84. * @memberof PerformanceWatchdog
  85. */
  86. PerformanceWatchdog.prototype.destroy = function () {
  87. this._viewModel.destroy();
  88. knockout.cleanNode(this._element);
  89. this._container.removeChild(this._element);
  90. return destroyObject(this);
  91. };
  92. export default PerformanceWatchdog;