grbsk / ng-idle

Responding to idle users in AngularJS applications.
http://hackedbychinese.github.io/ng-idle
MIT License
564 stars 195 forks source link

Cyclical dependency using ngIdle with http interceptors #102

Closed agentmerlin closed 8 years ago

agentmerlin commented 9 years ago

This may just be more of a feature request, but I was attempting to use the the Idle service to strictly interrupt on http requests. Here is an example of the Interceptor service.

angular.module('idleTest')
    .factory('IdleInterruptInterceptor', ['Idle', function (Idle) {

        // Manually interrupt the IdleService on every response
        return {
            response: function (response) {
                Idle.interrupt();
                return response;
            }
        };
    }]);

However, this is causing a circular dependency:

Uncaught Error: [$injector:cdep] Circular dependency found: $http <- Keepalive <- Idle <- IdleInterruptInterceptor <- $http

I'd love for the Idle service to be capable of being used with an interceptor, but I'm not sure the best way to go about adding that functionality in without dropping the dependency between Keepalive and $http or between Idle and Keepalive.

grbsk commented 9 years ago

The only thing I can think of at the moment without radical changes is to have Idle monitor an event such as Idle.interrupt on rootScope, and when a message is received, it will just call interrupt on itself. It feels a little hackish, but it may work.

toxaq commented 8 years ago

ngidle could just use an $injector to load $http itself, rather than using DI. $http = $injector.get('$http')

grbsk commented 8 years ago

That's true. He can also use $injector in his interceptor.

.factory('IdleInterruptInterceptor', ['$injector', function($injector) {

   // Manually interrupt the IdleService on every response
   return {
     response: function(response) {
       $injector.get('Idle').interrupt();
       return response;
     }
   };
 }])

I've mentioned in other places that a future release should focus on modularizing the current functionality of ng-idle so people can pick and choose what they actually need, but this seems like a viable alternative for now.

http://plnkr.co/edit/HZW580Welvt2T4acT1yD?p=preview

grbsk commented 8 years ago

I feel that adequate workarounds to make this work have been provided above.

abilashs90 commented 7 years ago

Hi @HackedByChinese, I have a scenario where I have to renew my idle every single time a request is fired. I tried interrupt and unwatch and immediately watch again in the interceptor. Because I did something in the interceptor, IdleTimeout is not triggered. Because of this, a legitimate timeout is not timing out.

Any solutions?