Insert the app sorted into the cache

This commit is contained in:
Girish Ramakrishnan
2018-06-26 17:12:55 -07:00
parent b41d0379f0
commit a833ceb737

View File

@@ -1089,9 +1089,23 @@ angular.module('Application').service('Client', ['$http', '$interval', 'md5', 'N
return app;
};
function binarySearch(array, pred) {
var lo = -1, hi = array.length;
while (1 + lo !== hi) {
var mi = lo + ((hi - lo) >> 1);
if (pred(array[mi])) {
hi = mi;
} else {
lo = mi;
}
}
return hi;
}
Client.prototype._updateAppCache = function (app) {
var found = -1;
// find by id and not by loc because the app's fqdn might have changed between invokations
for (var i = 0; i < this._installedApps.length; ++i) {
if (this._installedApps[i].id === app.id) {
found = i;
@@ -1105,10 +1119,13 @@ angular.module('Application').service('Client', ['$http', '$interval', 'md5', 'N
this._appPostProcess(tmp);
// only replace if the app is already known
if (found !== -1) {
if (found !== -1 && this._installedApps[found].fqdn === tmp.fqdn) { // app location has not changed
angular.copy(tmp, this._installedApps[found]);
} else {
this._installedApps.push(tmp);
if (found !== -1) this._installedApps.splice(found, 1); // remove it
var loc = binarySearch(this._installedApps, function (item) { return item.fqdn.localeCompare(app.fqdn) <= 0; });
this._installedApps.splice(loc, 0, tmp); // insert into sorted fqdn array
}
};
@@ -1149,8 +1166,6 @@ angular.module('Application').service('Client', ['$http', '$interval', 'md5', 'N
}
}
that._installedApps = that._installedApps.sort(function (app1, app2) { return app1.fqdn.localeCompare(app2.fqdn); });
callback(null);
});
};