PerformanceWatchdogViewModel.js 4.1 KB

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