瀏覽代碼

HUE-6087 [core] Add pauseable and resumeable intervals to the window object

Enrico Berti 8 年之前
父節點
當前提交
68821b0

+ 94 - 0
desktop/core/src/desktop/static/desktop/js/hue4.utils.js

@@ -0,0 +1,94 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+(function () {
+
+  var originalSetInterval = window.setInterval;
+  var originalClearInterval = window.clearInterval;
+  var hueIntervals = [];
+
+  /**
+   * @param {Function} fn - the function to be called at intervals
+   * @param {Number} timeout - timeout in milliseconds
+   * @param {string} [app] - context for the interval
+   */
+  window.setInterval = function (fn, timeout, app) {
+    var id = originalSetInterval(fn, timeout);
+    hueIntervals.push({
+      args: arguments,
+      id: id,
+      originalId: id,
+      status: 'running'
+    });
+    return id;
+  }
+
+  /**
+   * @param {Number} id - the original interval id generated by window.setInterval
+   */
+
+  window.clearInterval = function (id) {
+    var foundIntervals = hueIntervals.filter(function (obj) {
+      return obj.originalId === id
+    });
+    if (foundIntervals && foundIntervals.length > 0) {
+      originalClearInterval(foundIntervals[0].id);
+    }
+    else {
+      originalClearInterval(id);
+    }
+    hueIntervals = hueIntervals.filter(function (obj) {
+      return obj.originalId !== id
+    });
+  }
+
+  /**
+   * @param {string} app - context for the intervals to be suspended
+   */
+  window.pauseAppIntervals = function (app) {
+    hueIntervals.forEach(function (interval) {
+      if (interval.args[2] && interval.args[2] === app) {
+        interval.status = 'paused';
+        originalClearInterval(interval.id);
+      }
+    });
+  }
+
+  /**
+   * @param {string} app - context for the intervals to be restarted
+   */
+  window.resumeAppIntervals = function (app) {
+    hueIntervals.forEach(function (interval) {
+      if (interval.args[2] && interval.args[2] === app && interval.status === 'paused') {
+        interval.status = 'running';
+        var id = originalSetInterval(interval.args[0], interval.args[1]);
+        interval.id = id;
+      }
+    });
+  }
+
+  /**
+   * @param {string} app - context for the intervals to be cleared
+   */
+  window.clearAppIntervals = function (app) {
+    hueIntervals.forEach(function (interval) {
+      if (interval.args[2] && interval.args[2] === app) {
+        window.clearInterval(interval.originalId);
+      }
+    });
+  }
+
+})()

+ 70 - 0
desktop/core/src/desktop/static/desktop/spec/hue4UtilsSpec.js

@@ -0,0 +1,70 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+(function () {
+  describe("hue4.utils.js", function () {
+
+    beforeEach(function () {
+      intervalCallback = jasmine.createSpy("intervalCallback");
+    });
+
+    it("should accept both formats of window.setInterval", function () {
+      var id1 = window.setInterval(intervalCallback, 100);
+      var id2 = window.setInterval(intervalCallback, 100, 'jasmine');
+      expect(id1).toBeGreaterThan(0);
+      expect(id2).toBeGreaterThan(0);
+      window.clearInterval(id1);
+      window.clearInterval(id2);
+    });
+
+    it("should make the hue setInterval behave like the default window.setInterval", function () {
+      jasmine.clock().install();
+      var id = window.setInterval(intervalCallback, 10);
+      expect(intervalCallback).not.toHaveBeenCalled();
+      jasmine.clock().tick(11);
+      expect(intervalCallback).toHaveBeenCalled();
+      window.clearInterval(id);
+      jasmine.clock().uninstall();
+    });
+
+    it("should pause and resume all the intervals of an app", function (done) {
+      var id = window.setInterval(intervalCallback, 10, 'jasmine');
+      expect(intervalCallback).not.toHaveBeenCalled();
+      window.pauseAppIntervals('jasmine');
+      window.setTimeout(function () {
+        expect(intervalCallback).not.toHaveBeenCalled();
+        window.resumeAppIntervals('jasmine');
+        window.setTimeout(function () {
+          expect(intervalCallback).toHaveBeenCalled();
+          window.clearInterval(id);
+          done();
+        }, 20);
+      }, 20)
+    });
+
+    it("should clear the original interval id after a pause and resume", function (done) {
+      var id = window.setInterval(intervalCallback, 30, 'jasmine');
+      expect(intervalCallback).not.toHaveBeenCalled();
+      window.pauseAppIntervals('jasmine');
+      window.resumeAppIntervals('jasmine');
+      window.clearInterval(id);
+      window.setTimeout(function () {
+        expect(intervalCallback).not.toHaveBeenCalled();
+        done();
+      }, 20)
+    });
+
+  });
+})();

+ 2 - 0
desktop/core/src/desktop/templates/hue.mako

@@ -59,6 +59,8 @@
     var IS_HUE_4 = true;
   </script>
 
+  <script src="${ static('desktop/js/hue4.utils.js') }"></script>
+
 </head>
 
 <body>

+ 2 - 1
desktop/core/src/desktop/templates/jasmineRunner.html

@@ -61,6 +61,7 @@
   <script type="text/javascript" src="../static/desktop/js/jquery.migration.js"></script>
   <script type="text/javascript" src="../static/desktop/js/hue.colors.js"></script>
   <script type="text/javascript" src="../static/desktop/js/hue.utils.js"></script>
+  <script type="text/javascript" src="../static/desktop/js/hue4.utils.js"></script>
   <script type="text/javascript" src="../static/desktop/ext/js/jquery/plugins/jquery.total-storage.min.js"></script>
   <script type="text/javascript" src="../static/desktop/js/ace/ace.js"></script>
   <script type="text/javascript" src="../static/desktop/js/ace/ext-language_tools.js"></script>
@@ -75,8 +76,8 @@
   <script type="text/javascript" src="../static/desktop/ext/js/jasmine-2.3.4/boot.js"></script>
   <script type="text/javascript" src="../static/desktop/ext/js/jasmine-2.3.4/mock-ajax.js"></script>
 
-  <script type="text/javascript" src="../static/desktop/js/hue.utils.js"></script>
   <script type="text/javascript" src="../static/desktop/spec/hueUtilsSpec.js"></script>
+  <script type="text/javascript" src="../static/desktop/spec/hue4UtilsSpec.js"></script>
 
   <script type="text/javascript" src="../static/desktop/spec/apiHelperSpec.js"></script>