PerformanceWatchdogViewModel.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import defaultValue from "../../Core/defaultValue.js";
  2. import defined from "../../Core/defined.js";
  3. import destroyObject from "../../Core/destroyObject.js";
  4. import DeveloperError from "../../Core/DeveloperError.js";
  5. import FrameRateMonitor from "../../Scene/FrameRateMonitor.js";
  6. import knockout from "../../ThirdParty/knockout.js";
  7. import createCommand from "../createCommand.js";
  8. /**
  9. * The view model for {@link PerformanceWatchdog}.
  10. *
  11. * @alias PerformanceWatchdogViewModel
  12. * @constructor
  13. *
  14. * @param {Object} [options] Object with the following properties:
  15. * @param {Scene} options.scene The Scene instance 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 PerformanceWatchdogViewModel(options) {
  21. //>>includeStart('debug', pragmas.debug);
  22. if (!defined(options) || !defined(options.scene)) {
  23. throw new DeveloperError("options.scene is required.");
  24. }
  25. //>>includeEnd('debug');
  26. this._scene = options.scene;
  27. /**
  28. * Gets or sets the message to display when a low frame rate is detected. This string will be interpreted as HTML.
  29. * @type {String}
  30. */
  31. this.lowFrameRateMessage = defaultValue(
  32. options.lowFrameRateMessage,
  33. "This application appears to be performing poorly on your system. Please try using a different web browser or updating your video drivers."
  34. );
  35. /**
  36. * Gets or sets a value indicating whether the low frame rate message has previously been dismissed by the user. If it has
  37. * been dismissed, the message will not be redisplayed, no matter the frame rate.
  38. * @type {Boolean}
  39. */
  40. this.lowFrameRateMessageDismissed = false;
  41. /**
  42. * Gets or sets a value indicating whether the low frame rate message is currently being displayed.
  43. * @type {Boolean}
  44. */
  45. this.showingLowFrameRateMessage = false;
  46. knockout.track(this, [
  47. "lowFrameRateMessage",
  48. "lowFrameRateMessageDismissed",
  49. "showingLowFrameRateMessage",
  50. ]);
  51. const that = this;
  52. this._dismissMessage = createCommand(function () {
  53. that.showingLowFrameRateMessage = false;
  54. that.lowFrameRateMessageDismissed = true;
  55. });
  56. const monitor = FrameRateMonitor.fromScene(options.scene);
  57. this._unsubscribeLowFrameRate = monitor.lowFrameRate.addEventListener(
  58. function () {
  59. if (!that.lowFrameRateMessageDismissed) {
  60. that.showingLowFrameRateMessage = true;
  61. }
  62. }
  63. );
  64. this._unsubscribeNominalFrameRate = monitor.nominalFrameRate.addEventListener(
  65. function () {
  66. that.showingLowFrameRateMessage = false;
  67. }
  68. );
  69. }
  70. Object.defineProperties(PerformanceWatchdogViewModel.prototype, {
  71. /**
  72. * Gets the {@link Scene} instance for which to monitor performance.
  73. * @memberof PerformanceWatchdogViewModel.prototype
  74. * @type {Scene}
  75. */
  76. scene: {
  77. get: function () {
  78. return this._scene;
  79. },
  80. },
  81. /**
  82. * Gets a command that dismisses the low frame rate message. Once it is dismissed, the message
  83. * will not be redisplayed.
  84. * @memberof PerformanceWatchdogViewModel.prototype
  85. * @type {Command}
  86. */
  87. dismissMessage: {
  88. get: function () {
  89. return this._dismissMessage;
  90. },
  91. },
  92. });
  93. PerformanceWatchdogViewModel.prototype.destroy = function () {
  94. this._unsubscribeLowFrameRate();
  95. this._unsubscribeNominalFrameRate();
  96. return destroyObject(this);
  97. };
  98. export default PerformanceWatchdogViewModel;