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