optimize code
This commit is contained in:
parent
b00565fb1a
commit
256f429414
|
@ -16,22 +16,11 @@
|
|||
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);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var task = response.task;
|
||||
|
||||
if (task.status != 'active' || !task.bittorrent) {
|
||||
if (tabOrders.indexOf('btpeers') >= 0) {
|
||||
tabOrders.splice(tabOrders.indexOf('btpeers'), 1);
|
||||
|
@ -44,22 +33,69 @@
|
|||
|
||||
$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.selected = {};
|
||||
$rootScope.taskContext.selected[$scope.task.gid] = true;
|
||||
|
||||
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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Reference in a new issue