From 4e9404f29e2e0823990e9b7ed9167528312b3eae Mon Sep 17 00:00:00 2001 From: hamza zia Date: Mon, 18 Mar 2013 15:59:40 +0100 Subject: [PATCH] fixed angular recursive bug in nested rpc calls --- js/ctrls/download.js | 27 +++++++++++++++++++++++---- js/services/rpc/rpc.js | 13 ++++++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/js/ctrls/download.js b/js/ctrls/download.js index 3e8f193..7c5c11f 100644 --- a/js/ctrls/download.js +++ b/js/ctrls/download.js @@ -28,12 +28,27 @@ function( // put it in stopped list if active, // otherwise permanantly remove it // d: the download ctx - scope.remove = function(d) { + scope.remove = function(d, cb) { var method = 'remove'; + if (scope.getType(d) == 'stopped') 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) { @@ -47,8 +62,8 @@ function( }).value(); if (uris.length > 0) { - rpc.once('removeDownloadResult', [d.gid], function() { - rpc.once('addUri', uris); + scope.remove(d, function() { + rpc.once('addUri', uris, angular.noop, true); }); } } @@ -92,6 +107,10 @@ function( 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.splice( scope.pageSize ); diff --git a/js/services/rpc/rpc.js b/js/services/rpc/rpc.js index 3cdb2e5..c124561 100644 --- a/js/services/rpc/rpc.js +++ b/js/services/rpc/rpc.js @@ -59,20 +59,27 @@ function(syscall, time, alerts, utils, rootScope) { utils.setCookie('aria2conf', currentConf); + var cbs = []; _.each(data.result, function(d, i) { var handle = subscriptions[i]; if (handle) { if (d.code) { 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) { subscriptions[i] = null; } } }); - rootScope.$apply(); + + _.each(cbs, function(hnd) { + hnd.cb(hnd.data); + }); + + rootScope.$digest(); if (forceNextUpdate) { forceNextUpdate = false; @@ -122,7 +129,7 @@ function(syscall, time, alerts, utils, rootScope) { cb = cb || angular.noop; params = params || []; - subscriptions.push({ + subscriptions.unshift({ once: true, name: 'aria2.' + name, params: params,