ZabbixCachingProxy: Wrap _refresh() method to call it once.

This commit is contained in:
Alexander Zobnin
2016-01-31 16:20:09 +03:00
parent e9d29c63ba
commit 521e3a2a08

View File

@@ -26,28 +26,13 @@ function (angular, _, utils) {
this._initialized = undefined; this._initialized = undefined;
this.refreshPromise = false; this.refreshPromise = false;
// Wrap _refresh() method to call it once.
this.refresh = callOnce(p._refresh, this.refreshPromise);
} }
var p = ZabbixCachingProxy.prototype; var p = ZabbixCachingProxy.prototype;
/**
* Wrap _refresh() method to call it once.
*/
p.refresh = function() {
var self = this;
var deferred = $q.defer();
if (!self.refreshPromise) {
self.refreshPromise = deferred.promise;
self._refresh().then(function() {
deferred.resolve();
self.refreshPromise = null;
});
} else {
return self.refreshPromise;
}
return deferred.promise;
};
p._refresh = function() { p._refresh = function() {
var self = this; var self = this;
var promises = [ var promises = [
@@ -57,7 +42,7 @@ function (angular, _, utils) {
this.zabbixAPI.getItems() this.zabbixAPI.getItems()
]; ];
return $q.all(promises).then(function (results) { return $q.all(promises).then(function(results) {
if (results.length) { if (results.length) {
self._groups = results[0]; self._groups = results[0];
self._hosts = convertHosts(results[1]); self._hosts = convertHosts(results[1]);
@@ -122,7 +107,7 @@ function (angular, _, utils) {
* host.hosts - array of host ids * host.hosts - array of host ids
*/ */
function convertApplications(applications) { function convertApplications(applications) {
return _.map(_.groupBy(applications, 'name'), function (value, key) { return _.map(_.groupBy(applications, 'name'), function(value, key) {
return { return {
name: key, name: key,
applicationids: _.map(value, 'applicationid'), applicationids: _.map(value, 'applicationid'),
@@ -146,6 +131,22 @@ function (angular, _, utils) {
}); });
} }
function callOnce(func, promiseKeeper) {
return function() {
var deferred = $q.defer();
if (!promiseKeeper) {
promiseKeeper = deferred.promise;
func.apply(this, arguments).then(function(result) {
deferred.resolve(result);
promiseKeeper = null;
});
} else {
return promiseKeeper;
}
return deferred.promise;
};
}
return ZabbixCachingProxy; return ZabbixCachingProxy;
}); });