optimize code
This commit is contained in:
parent
b00565fb1a
commit
256f429414
|
@ -16,50 +16,86 @@
|
||||||
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 (task.status != 'active' || !task.bittorrent) {
|
||||||
if (!response.success) {
|
if (tabOrders.indexOf('btpeers') >= 0) {
|
||||||
if (response.data.message == aria2RpcErrors.Unauthorized.message) {
|
tabOrders.splice(tabOrders.indexOf('btpeers'), 1);
|
||||||
$interval.cancel(downloadTaskRefreshPromise);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (requireBtPeers(task)) {
|
||||||
|
aria2TaskService.getBtTaskPeers(task, function (response) {
|
||||||
if (task.status != 'active' || !task.bittorrent) {
|
if (response.success) {
|
||||||
if (tabOrders.indexOf('btpeers') >= 0) {
|
processPeers(response.data);
|
||||||
tabOrders.splice(tabOrders.indexOf('btpeers'), 1);
|
}
|
||||||
|
}, silent, includeLocalPeer);
|
||||||
}
|
}
|
||||||
}
|
}, silent);
|
||||||
|
} else {
|
||||||
if (!$scope.task || $scope.task.status != task.status) {
|
return aria2TaskService.getTaskStatusAndBtPeers($routeParams.gid, function (response) {
|
||||||
$scope.context.availableOptions = getAvailableOptions(task.status, !!task.bittorrent);
|
if (!response.success) {
|
||||||
}
|
return processError(response.data.message);
|
||||||
|
|
||||||
$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);
|
processTask(response.task);
|
||||||
}
|
processPeers(response.peers);
|
||||||
|
}, silent, requireBtPeers($scope.task), includeLocalPeer);
|
||||||
$rootScope.taskContext.list = [$scope.task];
|
}
|
||||||
$rootScope.taskContext.selected = {};
|
|
||||||
$rootScope.taskContext.selected[$scope.task.gid] = true;
|
|
||||||
|
|
||||||
ariaNgMonitorService.recordStat(task.gid, task);
|
|
||||||
}, silent, true);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var setSelectFiles = function (silent) {
|
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({
|
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);
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue