| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaCore UNA Core
- * @{
- */
- function BxDolLiveUpdates(oOptions)
- {
- this._sActionsUrl = oOptions.sActionsUrl == undefined ? sUrlRoot + 'live_updates.php' : oOptions.sActionsUrl;
- this._sObjName = oOptions.sObjName == undefined ? 'oLiveUpdates' : oOptions.sObjName;
- this._iInterval = oOptions.iInterval == undefined ? 3000 : oOptions.iInterval;
- this._aSystemsActive = oOptions.aSystemsActive == undefined ? {} : oOptions.aSystemsActive;
- this._aSystemsTransient = oOptions.aSystemsTransient == undefined ? {} : oOptions.aSystemsTransient;
- this._bServerRequesting = oOptions.bServerRequesting == undefined ? false : oOptions.bServerRequesting;
- this._sHash = oOptions.sHash == undefined ? '' : oOptions.sHash;
- this._iIndex = 0;
- this._iHandler = 0;
- this._bBusy = false;
- this._lastInactiveRun = 0;
- this.init();
- }
- BxDolLiveUpdates.prototype.init = function() {
- var $this = this;
- if(this._iHandler)
- this.destroy();
- $(document).ready(function() {
- $this.perform(); // make first call straight away to load addons for menus
- $this._iHandler = setInterval(function() {
- $this.perform();
- }, $this._iInterval);
- });
- };
- BxDolLiveUpdates.prototype.getSystemsTransient = function() {
- return this._aSystemsTransient;
- };
- BxDolLiveUpdates.prototype.setValue = function(sSystem, mixedValue) {
- if(sSystem && this._aSystemsActive[sSystem] != undefined && mixedValue)
- this._aSystemsActive[sSystem] = mixedValue;
- };
- BxDolLiveUpdates.prototype.add = function(oData) {
- if(!oData)
- return;
- if(oData.name != undefined && oData.value != undefined) {
- if(!this._aSystemsActive[oData.name])
- this._aSystemsActive[oData.name] = oData.value;
- if(!this._aSystemsTransient[oData.name])
- this._aSystemsTransient[oData.name] = 1;
- }
- if(oData.hash != undefined)
- this._sHash = oData.hash;
- };
- BxDolLiveUpdates.prototype.destroy = function() {
- if(this._iHandler)
- clearInterval(this._iHandler);
- };
- BxDolLiveUpdates.prototype.perform = function() {
- // if request already in progress, skip
- if (!this._bServerRequesting || this._bBusy) {
- return;
- }
- if ('undefined' !== typeof(document.hidden) && document.hidden) {
- // when hidden → allow execution only every 2h
- const now = Date.now();
- if (now - this._lastInactiveRun < 2 * 60 * 60 * 1000) {
- return; // not yet 2 hours
- }
- this._lastInactiveRun = now;
- } else {
- // when visible → reset timer
- this._lastInactiveRun = 0;
- }
- var $this = this;
- var oDate = new Date();
- this._bBusy = true;
- $.post(
- this._sActionsUrl,
- {
- index: this._iIndex,
- systems_active: this._aSystemsActive,
- systems_transient: this._aSystemsTransient,
- hash: this._sHash,
- _t: oDate.getTime()
- },
- function(aData) {
- $.each(aData, function(iIndex, oValue) {
- if(oValue.system && $this._aSystemsActive[oValue.system] != undefined && oValue.data)
- $this._aSystemsActive[oValue.system] = oValue.data.count_new;
- if(oValue.method) {
- var oFunc = function(oData) {
- eval(oValue.method);
- };
- oFunc(oValue.data);
- }
- });
- $this._bBusy = false;
- },
- 'json'
- );
- this._iIndex += 1;
- };
- /** @} */
|