Browse Source

avoid periodic polls when document doesn't have focus

avoids constantly pinging servers when the tab is in the background
Min RK 7 năm trước cách đây
mục cha
commit
24237fb5a2

+ 4 - 0
packages/filebrowser/src/model.ts

@@ -442,6 +442,10 @@ class FileBrowserModel implements IDisposable {
         this.refresh();
         return;
       }
+      if (!document.hasFocus()) {
+        // don't poll when nobody's looking
+        return;
+      }
       let date = new Date().getTime();
       if ((date - this._lastRefresh) > this._refreshDuration) {
         this.refresh();

+ 8 - 0
packages/services/src/kernel/manager.ts

@@ -44,9 +44,17 @@ class KernelManager implements Kernel.IManager {
 
     // Set up polling.
     this._modelsTimer = (setInterval as any)(() => {
+      if (typeof document !== 'undefined' && !document.hasFocus()) {
+        // don't poll when nobody's looking
+        return;
+      }
       this._refreshRunning();
     }, 10000);
     this._specsTimer = (setInterval as any)(() => {
+      if (typeof document !== 'undefined' && !document.hasFocus()) {
+        // don't poll when nobody's looking
+        return;
+      }
       this._refreshSpecs();
     }, 61000);
   }

+ 8 - 0
packages/services/src/session/manager.ts

@@ -48,9 +48,17 @@ class SessionManager implements Session.IManager {
 
     // Set up polling.
     this._modelsTimer = (setInterval as any)(() => {
+      if (typeof document !== 'undefined' && !document.hasFocus()) {
+        // don't poll when nobody's looking
+        return;
+      }
       this._refreshRunning();
     }, 10000);
     this._specsTimer = (setInterval as any)(() => {
+      if (typeof document !== 'undefined' && !document.hasFocus()) {
+        // don't poll when nobody's looking
+        return;
+      }
       this._refreshSpecs();
     }, 61000);
   }

+ 4 - 0
packages/services/src/terminal/manager.ts

@@ -40,6 +40,10 @@ class TerminalManager implements TerminalSession.IManager {
 
       // Set up polling.
       this._refreshTimer = (setInterval as any)(() => {
+        if (typeof document !== 'undefined' && !document.hasFocus()) {
+          // don't poll when nobody's looking
+          return;
+        }
         this._refreshRunning();
       }, 10000);
     }