我如何在工厂和服务之间进行沟通?

时间:2022-06-12 19:40:13

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

这个函数在ConnectService中。该函数每60秒检查一次连接:

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:

我有这个工厂方法检查http响应:

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?

我想做的是,如果有一个成功的响应,那么连接检查将被禁用60秒。有谁知道我如何在工厂和服务之间实现这种沟通吗?

1 个解决方案

#1


2  

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.

使用rootScope美元。$emit函数可以确保在$rootScope上监听事件的任何侦听器都知道该事件。

$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.

$ $通过范围层次结构将事件名向上发送,由于它是rootScope,所以它没有任何上位作用域,所以当您不确定范围层次结构时,它是最有效的通信方式。

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

也不要忘记使用$rootScope来侦听事件。在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)

#1


2  

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.

使用rootScope美元。$emit函数可以确保在$rootScope上监听事件的任何侦听器都知道该事件。

$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.

$ $通过范围层次结构将事件名向上发送,由于它是rootScope,所以它没有任何上位作用域,所以当您不确定范围层次结构时,它是最有效的通信方式。

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

也不要忘记使用$rootScope来侦听事件。在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)