user1679941
user1679941

Reputation:

How can I communicate between a factory and a service?

I have this function inside ConnectService. The function checks for a connection every 60 seconds:

class ConnectService {
    static $inject = ["$http","$interval","$q"];
    constructor(public $http, public $interval, public $q) { }
    checkConnection = () => {
            var self = this;
            let checking = false;
            self.isConnected().then(self.isConnectedHandler, self.isNotConnectedHandler);
            this.$interval(function () {
                // I need to somehow check if there has been
                // a http response in the last minute and if
                // so then return.
                if (checking) return; else checking = true;     
                self.isConnected().then(self.isConnectedHandler, self.isNotConnectedHandler)
                    .finally(() => {
                        checking = false;
                    });
            }, 60 * 1000);
        }
   }
  isConnectedHandler = (response): void => { // }
  isNotConnectedHandler = (response): void => { // }
}
app.service('connectService', ConnectService)

I have this factory method that checks http responses:

app.factory('testInterceptor', ['$q', function ($q) {      
    return {
        'response': function (response) {
            // I need to somehow set a flag or communicate to
            // the connection service not to do a check of a
            // connection for 60 seconds
            return response;
        }
    }
}]);

app.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push('testInterceptor');
}])

What I would like to do is to have it so that if there has been a successful response then the checking of connections is disabled for 60 seconds. Does anyone have any idea how I could achieve this communication between the factory and the service?

Upvotes: 2

Views: 83

Answers (1)

D_R
D_R

Reputation: 4962

I would suggest, in this case, that the services will not communicate with each other directly, or even be aware of each other. Services should be separated as much as possible.

Events would be more suitable in this case.

using the $rootScope.$emit function you could make sure that any listener that is listening to the event on the $rootScope will be aware of that event.

$emit dispatches an event name upwards through the scope hierarchy, and since it's the rootScope it doesn't have any upper scopes, so it's the most efficient way to communicate when you are not sure about the scope hierarchies.

also don't forget to listen to the event, using the $rootScope.$on in the ConnectService.

app.factory('testInterceptor', ['$q', '$rootScope', function ($q, $rootScope) {      
    return {
        'response': function (response) {
            $rootScope.$emit('rootScope:success-response');
            return response;
        }
    }
}]);

and the connect service:

class ConnectService {
    static $inject = ["$http","$interval","$q", "$rootScope"];
    constructor(public $http, public $interval, public $q, public $rootScope) {
      $rootScope.$on('rootScope:success-response',(event, data) => {
        // Delay the timer -> stop the interval and restart with delay
      });
    }
    checkConnection = () => {
            var self = this;
            let checking = false;
            self.isConnected().then(self.isConnectedHandler, self.isNotConnectedHandler);
            this.$interval(function () {
                // I need to somehow check if there has been
                // a http response in the last minute and if
                // so then return.
                if (checking) return; else checking = true;     
                self.isConnected().then(self.isConnectedHandler, self.isNotConnectedHandler)
                    .finally(() => {
                        checking = false;
                    });
            }, 60 * 1000);
        }
   }
  isConnectedHandler = (response): void => { // }
  isNotConnectedHandler = (response): void => { // }
}
app.service('connectService', ConnectService)

Upvotes: 2

Related Questions