Browse Source

Changes in api service

DSR! 3 năm trước cách đây
mục cha
commit
60726b4166
2 tập tin đã thay đổi với 27 bổ sung1 xóa
  1. 1 0
      README.md
  2. 26 1
      src/pineapple/js/services.js

+ 1 - 0
README.md

@@ -18,6 +18,7 @@ The following functionalities are modified looking for a better user experience
  - Added more refresh buttons
  - Fixed several bugs found in the panel
  - Expose AngularJS Pineapple API in JS window
+ - Add timeout and prevent duplicated request in API service
  - Project minification (Optional: use packer tools)
 
 ## Dashboard

+ 26 - 1
src/pineapple/js/services.js

@@ -1,12 +1,37 @@
 (function(){
     angular.module('pineapple')
+
+    .config(function ($provide, $httpProvider) {
+        $httpProvider.interceptors.push(['$injector', '$q', function interceptors($injector, $q) {
+            return {
+                request: function request(config) {
+                    var $http = $injector.get('$http');
+                    var _config = angular.copy(config);
+                    delete _config.headers;
+
+                    function isConfigEqual(pendingRequestConfig) {
+                        var _pendingRequestConfig = angular.copy(pendingRequestConfig);
+                        delete _pendingRequestConfig.headers;
+                        return angular.equals(_config, _pendingRequestConfig);
+                    }
+
+                    if ($http.pendingRequests.some(isConfigEqual)) {
+                        return $q.reject(request);
+                    }
+
+                    return config;
+                }
+            };
+        }]);
+    })
+
     .service('$api', ['$http', function($http){
         this.navbarReloader = false;
         this.device = undefined;
         this.deviceCallbacks = [];
 
         this.request = (function(data, callback, scope) {
-            return $http.post('/api/', data).
+            return $http.post('/api/', data, { timeout: 30000 }).
             then(function(response){
                 if (response.data.error === "Not Authenticated") {
                     if (response.data.setupRequired === true) {