Mastering Web Application

(Rick Simeone) #1
Chapter 7

Creating the securityRetryQueue service


The securityRetryQueue service provides a place to store all those items that
will be needed for retry, once the user authenticates successfully. It is basically
a list, to which you add a function (to be called, when the item is retried) with
pushRetryFn(). We process the items in the list by calling retryAll() or
cancelAll(). Here is the retryAll() method.


retryAll: function() {
while(retryQueue.length) {
retryQueue.shift().retry();
}
}

All items in the queue must have two methods, retry() and cancel(). The
pushRetryFn()method makes it easier to set up these objects, as shown in the
following code:


pushRetryFn: function(reason, retryFn) {
var deferredRetry = $q.defer();
var retryItem = {
reason: reason,
retry: function() {
$q.when(retryFn()).then(function(value) {
deferredRetry.resolve(value);
},function(value) {
deferredRetry.reject(value);
});
},
cancel: function() {
deferredRetry.reject();
}
};
service.push(retryItem);
return deferredRetry.promise;
}

This function returns a promise to retry the provided retryFn function. This retry
promise object will be resolved or rejected, when the item in the queue is retried.


We have to create our own new deferred object for this Retry
promise object because its resolution or rejection is triggered, not by the
response from the server, but by the call to retry() or cancel().
Free download pdf