PerformanceWatchdogViewModel.js 3.5 KB

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