BxDolLiveUpdates.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup UnaCore UNA Core
  6. * @{
  7. */
  8. function BxDolLiveUpdates(oOptions)
  9. {
  10. this._sActionsUrl = oOptions.sActionsUrl == undefined ? sUrlRoot + 'live_updates.php' : oOptions.sActionsUrl;
  11. this._sObjName = oOptions.sObjName == undefined ? 'oLiveUpdates' : oOptions.sObjName;
  12. this._iInterval = oOptions.iInterval == undefined ? 3000 : oOptions.iInterval;
  13. this._aSystemsActive = oOptions.aSystemsActive == undefined ? {} : oOptions.aSystemsActive;
  14. this._aSystemsTransient = oOptions.aSystemsTransient == undefined ? {} : oOptions.aSystemsTransient;
  15. this._bServerRequesting = oOptions.bServerRequesting == undefined ? false : oOptions.bServerRequesting;
  16. this._sHash = oOptions.sHash == undefined ? '' : oOptions.sHash;
  17. this._iIndex = 0;
  18. this._iHandler = 0;
  19. this._bBusy = false;
  20. this._lastInactiveRun = 0;
  21. this.init();
  22. }
  23. BxDolLiveUpdates.prototype.init = function() {
  24. var $this = this;
  25. if(this._iHandler)
  26. this.destroy();
  27. $(document).ready(function() {
  28. $this.perform(); // make first call straight away to load addons for menus
  29. $this._iHandler = setInterval(function() {
  30. $this.perform();
  31. }, $this._iInterval);
  32. });
  33. };
  34. BxDolLiveUpdates.prototype.getSystemsTransient = function() {
  35. return this._aSystemsTransient;
  36. };
  37. BxDolLiveUpdates.prototype.setValue = function(sSystem, mixedValue) {
  38. if(sSystem && this._aSystemsActive[sSystem] != undefined && mixedValue)
  39. this._aSystemsActive[sSystem] = mixedValue;
  40. };
  41. BxDolLiveUpdates.prototype.add = function(oData) {
  42. if(!oData)
  43. return;
  44. if(oData.name != undefined && oData.value != undefined) {
  45. if(!this._aSystemsActive[oData.name])
  46. this._aSystemsActive[oData.name] = oData.value;
  47. if(!this._aSystemsTransient[oData.name])
  48. this._aSystemsTransient[oData.name] = 1;
  49. }
  50. if(oData.hash != undefined)
  51. this._sHash = oData.hash;
  52. };
  53. BxDolLiveUpdates.prototype.destroy = function() {
  54. if(this._iHandler)
  55. clearInterval(this._iHandler);
  56. };
  57. BxDolLiveUpdates.prototype.perform = function() {
  58. // if request already in progress, skip
  59. if (!this._bServerRequesting || this._bBusy) {
  60. return;
  61. }
  62. if ('undefined' !== typeof(document.hidden) && document.hidden) {
  63. // when hidden → allow execution only every 2h
  64. const now = Date.now();
  65. if (now - this._lastInactiveRun < 2 * 60 * 60 * 1000) {
  66. return; // not yet 2 hours
  67. }
  68. this._lastInactiveRun = now;
  69. } else {
  70. // when visible → reset timer
  71. this._lastInactiveRun = 0;
  72. }
  73. var $this = this;
  74. var oDate = new Date();
  75. this._bBusy = true;
  76. $.post(
  77. this._sActionsUrl,
  78. {
  79. index: this._iIndex,
  80. systems_active: this._aSystemsActive,
  81. systems_transient: this._aSystemsTransient,
  82. hash: this._sHash,
  83. _t: oDate.getTime()
  84. },
  85. function(aData) {
  86. $.each(aData, function(iIndex, oValue) {
  87. if(oValue.system && $this._aSystemsActive[oValue.system] != undefined && oValue.data)
  88. $this._aSystemsActive[oValue.system] = oValue.data.count_new;
  89. if(oValue.method) {
  90. var oFunc = function(oData) {
  91. eval(oValue.method);
  92. };
  93. oFunc(oValue.data);
  94. }
  95. });
  96. $this._bBusy = false;
  97. },
  98. 'json'
  99. );
  100. this._iIndex += 1;
  101. };
  102. /** @} */