From 256f4294145327c4a25c9daa63011dd3d1de2308 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Tue, 5 Jul 2016 00:59:04 +0800 Subject: [PATCH] optimize code --- src/scripts/controllers/task-detail.js | 106 +++++++++++++++-------- src/scripts/services/aria2TaskService.js | 19 ++-- 2 files changed, 83 insertions(+), 42 deletions(-) diff --git a/src/scripts/controllers/task-detail.js b/src/scripts/controllers/task-detail.js index 7916324..a281ba1 100644 --- a/src/scripts/controllers/task-detail.js +++ b/src/scripts/controllers/task-detail.js @@ -16,50 +16,86 @@ return aria2SettingService.getSpecifiedOptions(keys); }; - var refreshDownloadTask = function (silent) { - if (pauseDownloadTaskRefresh) { + var processTask = function (task) { + if (!task) { return; } - return aria2TaskService.getTaskStatusAndBtPeers($routeParams.gid, function (response) { - if (!response.success) { - if (response.data.message == aria2RpcErrors.Unauthorized.message) { - $interval.cancel(downloadTaskRefreshPromise); + if (task.status != 'active' || !task.bittorrent) { + if (tabOrders.indexOf('btpeers') >= 0) { + tabOrders.splice(tabOrders.indexOf('btpeers'), 1); + } + } + + if (!$scope.task || $scope.task.status != task.status) { + $scope.context.availableOptions = getAvailableOptions(task.status, !!task.bittorrent); + } + + $scope.task = ariaNgCommonService.copyObjectTo(task, $scope.task); + + $rootScope.taskContext.list = [$scope.task]; + $rootScope.taskContext.selected = {}; + $rootScope.taskContext.selected[$scope.task.gid] = true; + + ariaNgMonitorService.recordStat(task.gid, task); + }; + + var processPeers = function (peers) { + if (!peers) { + return; + } + + if (!ariaNgCommonService.extendArray(peers, $scope.context.btPeers, 'peerId')) { + $scope.context.btPeers = peers; + } + + $scope.context.healthPercent = aria2TaskService.estimateHealthPercentFromPeers($scope.task, $scope.context.btPeers); + }; + + var requireBtPeers = function (task) { + return (task && task.bittorrent && task.status == 'active'); + }; + + var refreshDownloadTask = function (silent) { + if (pauseDownloadTaskRefresh) { + return; + } + + var processError = function (message) { + if (message == aria2RpcErrors.Unauthorized.message) { + $interval.cancel(downloadTaskRefreshPromise); + } + }; + + var includeLocalPeer = true; + + if (!$scope.task) { + return aria2TaskService.getTaskStatus($routeParams.gid, function (response) { + if (!response.success) { + return processError(response.data.message); } - return; - } + var task = response.data; + processTask(task); - var task = response.task; - - if (task.status != 'active' || !task.bittorrent) { - if (tabOrders.indexOf('btpeers') >= 0) { - tabOrders.splice(tabOrders.indexOf('btpeers'), 1); + if (requireBtPeers(task)) { + aria2TaskService.getBtTaskPeers(task, function (response) { + if (response.success) { + processPeers(response.data); + } + }, silent, includeLocalPeer); } - } - - if (!$scope.task || $scope.task.status != task.status) { - $scope.context.availableOptions = getAvailableOptions(task.status, !!task.bittorrent); - } - - $scope.task = ariaNgCommonService.copyObjectTo(task, $scope.task); - - if (response.peers) { - var peers = response.peers; - - if (!ariaNgCommonService.extendArray(peers, $scope.context.btPeers, 'peerId')) { - $scope.context.btPeers = peers; + }, silent); + } else { + return aria2TaskService.getTaskStatusAndBtPeers($routeParams.gid, function (response) { + if (!response.success) { + return processError(response.data.message); } - $scope.context.healthPercent = aria2TaskService.estimateHealthPercentFromPeers(task, $scope.context.btPeers); - } - - $rootScope.taskContext.list = [$scope.task]; - $rootScope.taskContext.selected = {}; - $rootScope.taskContext.selected[$scope.task.gid] = true; - - ariaNgMonitorService.recordStat(task.gid, task); - }, silent, true); + processTask(response.task); + processPeers(response.peers); + }, silent, requireBtPeers($scope.task), includeLocalPeer); + } }; var setSelectFiles = function (silent) { diff --git a/src/scripts/services/aria2TaskService.js b/src/scripts/services/aria2TaskService.js index 3a5aed4..5f1d354 100644 --- a/src/scripts/services/aria2TaskService.js +++ b/src/scripts/services/aria2TaskService.js @@ -302,12 +302,17 @@ } }); }, - getTaskStatusAndBtPeers: function (gid, callback, silent, includeLocalPeer) { + getTaskStatusAndBtPeers: function (gid, callback, silent, requirePeers, includeLocalPeer) { + var methods = [ + aria2RpcService.tellStatus({ gid: gid }, true) + ]; + + if (requirePeers) { + methods.push(aria2RpcService.getPeers({ gid: gid }, true)); + } + return aria2RpcService.multicall({ - methods: [ - aria2RpcService.tellStatus({ gid: gid }, true), - aria2RpcService.getPeers({ gid: gid }, true) - ], + methods: methods, silent: !!silent, callback: function (response) { if (!callback) { @@ -316,12 +321,12 @@ response.task = {}; - if (response.success) { + if (response.success && response.data.length > 0) { response.task = response.data[0][0]; processDownloadTask(response.task); } - if (response.success && response.task.bittorrent) { + if (response.success && response.task.bittorrent && response.data.length > 1) { response.peers = response.data[1][0]; processBtPeers(response.peers, response.task, includeLocalPeer); }