Convert `Timer/Repeating` to TypeScript
authorAlexander Ebert <ebert@woltlab.com>
Fri, 16 Oct 2020 15:24:18 +0000 (17:24 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 28 Oct 2020 11:28:54 +0000 (12:28 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Timer/Repeating.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Timer/Repeating.ts [new file with mode: 0644]

index 71eed566c52aa6884028e277bb7b65c5062a764c..e7155606b8025e7f2fcaf7a4766b583acf447134 100644 (file)
@@ -1,70 +1,54 @@
 /**
  * Provides an object oriented API on top of `setInterval`.
- * 
- * @author     Tim Duesterhus
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module     WoltLabSuite/Core/Timer/Repeating
+ *
+ * @author  Tim Duesterhus
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  WoltLabSuite/Core/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;
+define(["require", "exports"], function (require, exports) {
+    "use strict";
+    class RepeatingTimer {
+        /**
+         * 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(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.restart();
+        }
+        /**
+         * Stops the timer and restarts it. The next call will occur in `delta` milliseconds.
+         */
+        restart() {
+            this.stop();
+            this._timer = setInterval(this._callback, this._delta);
+        }
+        /**
+         * Stops the timer. It will no longer be called until you call `restart`.
+         */
+        stop() {
+            if (this._timer !== undefined) {
+                clearInterval(this._timer);
+                this._timer = undefined;
+            }
+        }
+        /**
+         * Changes the `delta` of the timer and `restart`s it.
+         */
+        setDelta(delta) {
+            this._delta = delta;
+            this.restart();
+        }
+    }
+    return RepeatingTimer;
 });
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Timer/Repeating.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Timer/Repeating.ts
new file mode 100644 (file)
index 0000000..360769f
--- /dev/null
@@ -0,0 +1,65 @@
+/**
+ * Provides an object oriented API on top of `setInterval`.
+ *
+ * @author  Tim Duesterhus
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  WoltLabSuite/Core/Timer/Repeating
+ */
+
+class RepeatingTimer {
+  private readonly _callback: (timer: RepeatingTimer) => void;
+  private _delta: number;
+  private _timer: number | undefined;
+
+  /**
+   * 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(callback: (timer: RepeatingTimer) => void, delta: number) {
+    if (typeof callback !== 'function') {
+      throw new TypeError("Expected a valid callback as first argument.");
+    }
+    if (delta < 0 || delta > 86_400 * 1_000) {
+      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.restart();
+  }
+
+  /**
+   * Stops the timer and restarts it. The next call will occur in `delta` milliseconds.
+   */
+  restart(): void {
+    this.stop();
+
+    this._timer = setInterval(this._callback, this._delta);
+  }
+
+  /**
+   * Stops the timer. It will no longer be called until you call `restart`.
+   */
+  stop(): void {
+    if (this._timer !== undefined) {
+      clearInterval(this._timer);
+
+      this._timer = undefined;
+    }
+  }
+
+  /**
+   * Changes the `delta` of the timer and `restart`s it.
+   */
+  setDelta(delta: number): void {
+    this._delta = delta;
+
+    this.restart();
+  }
+}
+
+export = RepeatingTimer;