optimize code

This commit is contained in:
MaysWind 2016-07-05 00:59:04 +08:00
parent b00565fb1a
commit 256f429414
2 changed files with 83 additions and 42 deletions

View file

@ -16,22 +16,11 @@
return aria2SettingService.getSpecifiedOptions(keys); return aria2SettingService.getSpecifiedOptions(keys);
}; };
var refreshDownloadTask = function (silent) { var processTask = function (task) {
if (pauseDownloadTaskRefresh) { if (!task) {
return; return;
} }
return aria2TaskService.getTaskStatusAndBtPeers($routeParams.gid, function (response) {
if (!response.success) {
if (response.data.message == aria2RpcErrors.Unauthorized.message) {
$interval.cancel(downloadTaskRefreshPromise);
}
return;
}
var task = response.task;
if (task.status != 'active' || !task.bittorrent) { if (task.status != 'active' || !task.bittorrent) {
if (tabOrders.indexOf('btpeers') >= 0) { if (tabOrders.indexOf('btpeers') >= 0) {
tabOrders.splice(tabOrders.indexOf('btpeers'), 1); tabOrders.splice(tabOrders.indexOf('btpeers'), 1);
@ -44,22 +33,69 @@
$scope.task = ariaNgCommonService.copyObjectTo(task, $scope.task); $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;
}
$scope.context.healthPercent = aria2TaskService.estimateHealthPercentFromPeers(task, $scope.context.btPeers);
}
$rootScope.taskContext.list = [$scope.task]; $rootScope.taskContext.list = [$scope.task];
$rootScope.taskContext.selected = {}; $rootScope.taskContext.selected = {};
$rootScope.taskContext.selected[$scope.task.gid] = true; $rootScope.taskContext.selected[$scope.task.gid] = true;
ariaNgMonitorService.recordStat(task.gid, task); ariaNgMonitorService.recordStat(task.gid, task);
}, silent, true); };
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);
}
var task = response.data;
processTask(task);
if (requireBtPeers(task)) {
aria2TaskService.getBtTaskPeers(task, function (response) {
if (response.success) {
processPeers(response.data);
}
}, silent, includeLocalPeer);
}
}, silent);
} else {
return aria2TaskService.getTaskStatusAndBtPeers($routeParams.gid, function (response) {
if (!response.success) {
return processError(response.data.message);
}
processTask(response.task);
processPeers(response.peers);
}, silent, requireBtPeers($scope.task), includeLocalPeer);
}
}; };
var setSelectFiles = function (silent) { var setSelectFiles = function (silent) {

View file

@ -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({ return aria2RpcService.multicall({
methods: [ methods: methods,
aria2RpcService.tellStatus({ gid: gid }, true),
aria2RpcService.getPeers({ gid: gid }, true)
],
silent: !!silent, silent: !!silent,
callback: function (response) { callback: function (response) {
if (!callback) { if (!callback) {
@ -316,12 +321,12 @@
response.task = {}; response.task = {};
if (response.success) { if (response.success && response.data.length > 0) {
response.task = response.data[0][0]; response.task = response.data[0][0];
processDownloadTask(response.task); 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]; response.peers = response.data[1][0];
processBtPeers(response.peers, response.task, includeLocalPeer); processBtPeers(response.peers, response.task, includeLocalPeer);
} }