Move cancelInterval into its own method to remove code duplication

This commit is contained in:
Joseph Schorr 2014-09-03 12:10:36 -04:00
parent 2cfab6e252
commit 232e3cc1da

View file

@ -2278,24 +2278,29 @@ quayApp.directive('signinForm', function () {
}
};
$scope.$watch('user.username', function() {
$scope.cancelInterval = function() {
$scope.tryAgainSoon = 0;
if ($scope.tryAgainInterval) {
$interval.cancel($scope.tryAgainInterval);
}
$scope.tryAgainInterval = null;
};
$scope.$watch('user.username', function() {
$scope.cancelInterval();
});
$scope.$on('$destroy', function() {
if ($scope.tryAgainInterval) {
$interval.cancel($scope.tryAgainInterval);
}
$scope.cancelInterval();
});
$scope.signin = function() {
if ($scope.tryAgainSoon > 0) { return; }
$scope.markStarted();
$scope.cancelInterval();
ApiService.signinUser($scope.user).then(function() {
$scope.needsEmailVerification = false;
@ -2318,24 +2323,18 @@ quayApp.directive('signinForm', function () {
}, 500);
}, function(result) {
if (result.status == 429 /* try again later */) {
$scope.needsEmailVerification = false;
$scope.invalidCredentials = false;
$scope.cancelInterval();
$scope.tryAgainSoon = result.headers('Retry-After');
// Cancel any existing interval.
if ($scope.tryAgainInterval) {
$interval.cancel($scope.tryAgainInterval);
}
// Setup a new interval.
$scope.tryAgainInterval = $interval(function() {
$scope.tryAgainSoon--;
if ($scope.tryAgainSoon <= 0) {
$scope.tryAgainInterval = null;
$scope.tryAgainSoon = 0;
$scope.cancelInterval();
}
}, 1000, $scope.tryAgainSoon);
$scope.needsEmailVerification = false;
$scope.invalidCredentials = false;
} else {
$scope.needsEmailVerification = result.data.needsEmailVerification;
$scope.invalidCredentials = result.data.invalidCredentials;