Add WoltLab/WCF/Timer/Repeating
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 18 May 2015 23:41:06 +0000 (01:41 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 18 May 2015 23:41:06 +0000 (01:41 +0200)
wcfsetup/install/files/js/WCF.js
wcfsetup/install/files/js/WoltLab/WCF/Template.js
wcfsetup/install/files/js/WoltLab/WCF/Timer/Repeating.js [new file with mode: 0644]

index 6a7618684708cd98db669156c992719c91401c93..ce88b235502ee198d81da8f4a6203b576d915e25 100755 (executable)
@@ -1962,12 +1962,7 @@ WCF.Clipboard = {
 };
 
 /**
- * Provides a simple call for periodical executed functions. Based upon
- * ideas by Prototype's PeriodicalExecuter.
- * 
- * @see                https://github.com/sstephenson/prototype/blob/master/src/prototype/lang/periodical_executer.js
- * @param      function                callback
- * @param      integer                 delay
+ * @deprecated Use WoltLab/WCF/Timer/Repeating
  */
 WCF.PeriodicalExecuter = Class.extend({
        /**
index 2ce9afd4d9694248dd2e547e490dbd81d37e3f59..ea28a33d126127fc705aaaea24e28633130c8f35 100644 (file)
@@ -42,7 +42,7 @@ define(['./Template.grammar', './StringUtil', 'Language'], function(parser, Stri
                        + "v.__wcf = window.WCF; v.__window = window;\n"
                        + "return " + template;
                        
-                       this.fetch = new Function("StringUtil", "Language", "v", template).bind(null, StringUtil, Language);
+                       this.fetch = new Function("StringUtil", "Language", "v", template).bind(undefined, StringUtil, Language);
                }
                catch (e) {
                        console.debug(e.message);
diff --git a/wcfsetup/install/files/js/WoltLab/WCF/Timer/Repeating.js b/wcfsetup/install/files/js/WoltLab/WCF/Timer/Repeating.js
new file mode 100644 (file)
index 0000000..baa94f7
--- /dev/null
@@ -0,0 +1,70 @@
+/**
+ * Provides an object oriented API on top of `setInterval`.
+ * 
+ * @author     Tim Duesterhus
+ * @copyright  2001-2015 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module     WoltLab/WCF/Timer/Repeating
+ */
+define([], function() {
+       "use strict";
+       
+       /**
+        * Creates a new timer that executes the given `callback` every `delta` milliseconds.
+        * It will be created in started mode. Call `stop()` if necessary.
+        * The `callback` will be passed the owning instance of `Repeating`.
+        * 
+        * @constructor
+        * @param       {function(Repeating)}   callback
+        * @param       {int}                   delta
+        */
+       function Repeating(callback, delta) {
+               if (typeof callback !== 'function') {
+                       throw new TypeError("Expected a valid callback as first argument.");
+               }
+               if (delta < 0 || delta > 86400 * 1000) {
+                       throw new RangeError("Invalid delta " + delta + ". Delta must be in the interval [0, 86400000].");
+               }
+               
+               // curry callback with `this` as the first parameter
+               this._callback = callback.bind(undefined, this);
+               
+               this._delta = delta;
+               this._timer = undefined;
+               
+               this.restart();
+       };
+       Repeating.prototype = {
+               /**
+                * Stops the timer and restarts it. The next call will occur in `delta` milliseconds.
+                */
+               restart: function() {
+                       this.stop();
+                       
+                       this._timer = setInterval(this._callback, this._delta);
+               },
+               
+               /**
+                * Stops the timer. It will no longer be called until you call `restart`.
+                */
+               stop: function() {
+                       if (this._timer !== undefined) {
+                               clearInterval(this._timer);
+                               this._timer = undefined;
+                       }
+               },
+               
+               /**
+                * Changes the `delta` of the timer and `restart`s it.
+                * 
+                * @param       {int}   delta   New delta of the timer.
+                */
+               setDelta: function(delta) {
+                       this._delta = delta;
+                       
+                       this.restart();
+               }
+       };
+       
+       return Repeating;
+});