Only poll for checks if tab is in focus

Allows check state chip and table to refresh automatically only if the checks tab is
in focus.
Fix issues from rolled back commit : I999144dcc
  * ensure multiple listeners are not added
  * unsubscribe listener when component is detached
  * clear polling intervals when component is detached

Change-Id: Iaad5a54878b99f8f544176045acc19e073939f3c
diff --git a/gr-checks/gr-checks-chip-view.js b/gr-checks/gr-checks-chip-view.js
index e96cdd3..cd07acb 100644
--- a/gr-checks/gr-checks-chip-view.js
+++ b/gr-checks/gr-checks-chip-view.js
@@ -84,7 +84,16 @@
         type: Boolean,
         value: false
       },
-      pollChecksInterval: Object
+      pollChecksInterval: Object,
+      visibilityChangeListenerAdded: {
+        type: Boolean,
+        value: false
+      }
+    },
+
+    detached() {
+      clearInterval(this.pollChecksInterval);
+      this.unlisten(document, 'visibilitychange', '_onVisibililityChange');
     },
 
     observers: [
@@ -124,6 +133,14 @@
       });
     },
 
+    _onVisibililityChange() {
+      if (document.hidden) {
+        clearInterval(this.pollChecksInterval);
+        return;
+      }
+      this._pollChecksRegularly(this.change, this.revision, this.getChecks);
+    },
+
     _pollChecksRegularly(change, revision, getChecks) {
       if (this.pollChecksInterval) {
         clearInterval(this.pollChecksInterval);
@@ -131,6 +148,10 @@
       const poll = () => this._fetchChecks(change, revision, getChecks);
       poll();
       this.pollChecksInterval = setInterval(poll, CHECKS_POLL_INTERVAL_MS)
+      if (!this.visibilityChangeListenerAdded) {
+        this.visibilityChangeListenerAdded = true;
+        this.listen(document, 'visibilitychange', '_onVisibililityChange');
+      }
     },
 
     /**
diff --git a/gr-checks/gr-checks-view.js b/gr-checks/gr-checks-view.js
index 43643f0..f5d083f 100644
--- a/gr-checks/gr-checks-view.js
+++ b/gr-checks/gr-checks-view.js
@@ -53,13 +53,21 @@
         type: Object,
         value: LoadingStatus.LOADING,
       },
-      pollChecksInterval: Object
+      pollChecksInterval: Object,
+      visibilityChangeListenerAdded: {
+        type: Boolean,
+        value: false
+      }
     },
 
     observers: [
       '_pollChecksRegularly(change, revision, getChecks)',
     ],
 
+    detached() {
+      clearInterval(this.pollChecksInterval);
+      this.unlisten(document, 'visibilitychange', '_onVisibililityChange');
+    },
 
     _orderChecks(a, b) {
       if (a.state != b.state) {
@@ -95,6 +103,14 @@
       });
     },
 
+    _onVisibililityChange() {
+      if (document.hidden) {
+        clearInterval(this.pollChecksInterval);
+        return;
+      }
+      this._pollChecksRegularly(this.change, this.revision, this.getChecks);
+    },
+
     _pollChecksRegularly(change, revision, getChecks) {
       if (this.pollChecksInterval) {
         clearInterval(this.pollChecksInterval);
@@ -102,6 +118,10 @@
       const poll = () => this._fetchChecks(change, revision, getChecks);
       poll();
       this.pollChecksInterval = setInterval(poll, CHECKS_POLL_INTERVAL_MS)
+      if (!this.visibilityChangeListenerAdded) {
+        this.visibilityChangeListenerAdded = true;
+        this.listen(document, 'visibilitychange', '_onVisibililityChange');
+      }
     },
 
     _checkConfigured() {