fixed angular recursive bug in nested rpc calls

This commit is contained in:
hamza zia 2013-03-18 15:59:40 +01:00
parent 8b3b30dd4a
commit 4e9404f29e
2 changed files with 33 additions and 7 deletions

View File

@ -28,12 +28,27 @@ function(
// put it in stopped list if active, // put it in stopped list if active,
// otherwise permanantly remove it // otherwise permanantly remove it
// d: the download ctx // d: the download ctx
scope.remove = function(d) { scope.remove = function(d, cb) {
var method = 'remove'; var method = 'remove';
if (scope.getType(d) == 'stopped') if (scope.getType(d) == 'stopped')
method = 'removeDownloadResult'; method = 'removeDownloadResult';
rpc.once(method, [d.gid]); rpc.once(method, [d.gid], cb);
// also remove it from client cache assuming that it will be deleted in the aria2 list,
// but we could be wrong but the cache will update in next global update
var downloads = [scope.active, scope.waiting, scope.stopped], ind = -1, i;
for (i = 0; i < downloads.length; i++) {
ind = downloads[i].indexOf(d);
if (ind != -1) break;
}
if (ind == -1) {
return;
}
downloads[i].splice(ind, 1);
} }
scope.restart = function(d) { scope.restart = function(d) {
@ -47,8 +62,8 @@ function(
}).value(); }).value();
if (uris.length > 0) { if (uris.length > 0) {
rpc.once('removeDownloadResult', [d.gid], function() { scope.remove(d, function() {
rpc.once('addUri', uris); rpc.once('addUri', uris, angular.noop, true);
}); });
} }
} }
@ -92,6 +107,10 @@ function(
scope.totalPages = Math.ceil(scope.totalDownloads / scope.pageSize) scope.totalPages = Math.ceil(scope.totalDownloads / scope.pageSize)
// fix the bug when downloads are deleted until no left on a specific page
if (scope.currentPage > scope.totalPages)
scope.currentPage = scope.totalPages;
downloads = downloads.slice( (scope.currentPage - 1) * scope.pageSize ); downloads = downloads.slice( (scope.currentPage - 1) * scope.pageSize );
downloads.splice( scope.pageSize ); downloads.splice( scope.pageSize );

View File

@ -59,20 +59,27 @@ function(syscall, time, alerts, utils, rootScope) {
utils.setCookie('aria2conf', currentConf); utils.setCookie('aria2conf', currentConf);
var cbs = [];
_.each(data.result, function(d, i) { _.each(data.result, function(d, i) {
var handle = subscriptions[i]; var handle = subscriptions[i];
if (handle) { if (handle) {
if (d.code) { if (d.code) {
alerts.addAlert(d.message, 'error'); alerts.addAlert(d.message, 'error');
} }
handle.cb(d); // run them later as the cb itself can mutate the subscriptions
cbs.push({cb: handle.cb, data: d});
if (handle.once) { if (handle.once) {
subscriptions[i] = null; subscriptions[i] = null;
} }
} }
}); });
rootScope.$apply();
_.each(cbs, function(hnd) {
hnd.cb(hnd.data);
});
rootScope.$digest();
if (forceNextUpdate) { if (forceNextUpdate) {
forceNextUpdate = false; forceNextUpdate = false;
@ -122,7 +129,7 @@ function(syscall, time, alerts, utils, rootScope) {
cb = cb || angular.noop; cb = cb || angular.noop;
params = params || []; params = params || [];
subscriptions.push({ subscriptions.unshift({
once: true, once: true,
name: 'aria2.' + name, name: 'aria2.' + name,
params: params, params: params,