ESPUtil.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*##############################################################################
  2. # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################## */
  16. define([
  17. "dojo/_base/declare",
  18. "dojo/Stateful"
  19. ], function (declare, Stateful) {
  20. var SingletonData = declare([Stateful], {
  21. // Attributes ---
  22. _hasCompletedSetter: function (hasCompleted) {
  23. if (this.hasCompleted !== hasCompleted) {
  24. this.hasCompleted = hasCompleted;
  25. if (!this.hasCompleted) {
  26. if (this.startMonitor) {
  27. this.startMonitor();
  28. }
  29. } else {
  30. if (this.stopMonitor) {
  31. this.stopMonitor();
  32. }
  33. }
  34. }
  35. },
  36. // Methods ---
  37. constructor: function (args) {
  38. this.changedCount = 0;
  39. this._changedCache = {};
  40. },
  41. getData: function () {
  42. if (this instanceof SingletonData) {
  43. return (SingletonData)(this);
  44. }
  45. return {};
  46. },
  47. updateData: function (response) {
  48. var changed = false;
  49. for (var key in response) {
  50. var json = dojo.toJson(response[key]);
  51. if (this._changedCache[key] !== json) {
  52. this._changedCache[key] = json;
  53. this.set(key, response[key]);
  54. changed = true;
  55. }
  56. }
  57. if (changed) {
  58. this.set("changedCount", this.get("changedCount") + 1);
  59. }
  60. }
  61. });
  62. var Monitor = declare(null, {
  63. startMonitor: function () {
  64. this._timerTickCount = 0;
  65. this._timer = 1000;
  66. this.onMonitor();
  67. },
  68. stopMonitor: function () {
  69. this._timerTickCount = 0;
  70. this._timer = 0;
  71. },
  72. onMonitor: function () {
  73. this._timerTickCount++;
  74. if (this.hasCompleted) {
  75. this.stopMonitor();
  76. return;
  77. } else {
  78. if (this._timerTickCount === 1) {
  79. this.refresh(true);
  80. } else if (this._timerTickCount < 5 && this._timerTickCount % 1 === 0) {
  81. this.refresh();
  82. } else if (this._timerTickCount < 30 && this._timerTickCount % 5 === 0) {
  83. this.refresh();
  84. } else if (this._timerTickCount < 60 && this._timerTickCount % 10 === 0) {
  85. this.refresh();
  86. } else if (this._timerTickCount < 120 && this._timerTickCount % 30 === 0) {
  87. this.refresh(true);
  88. } else if (this._timerTickCount % 60 === 0) {
  89. this.refresh(true);
  90. }
  91. }
  92. var context = this;
  93. if (this._timer) {
  94. setTimeout(function () {
  95. context.onMonitor();
  96. }, this._timer);
  97. } else {
  98. this._timerTickCount = 0;
  99. }
  100. }
  101. });
  102. return {
  103. Singleton: SingletonData,
  104. Monitor: Monitor
  105. };
  106. });