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,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) {

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({
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);
}