]> git.basschouten.com Git - openhab-addons.git/commitdiff
[jsscripting] Implement NodeJS-like parameter handling for timer polyfills (#15193)
authorFlorian Hotze <florianh_dev@icloud.com>
Fri, 7 Jul 2023 21:10:35 +0000 (23:10 +0200)
committerGitHub <noreply@github.com>
Fri, 7 Jul 2023 21:10:35 +0000 (23:10 +0200)
* [jsscripting] Implement NodeJS-like param handling for timer polyfills
* [jsscripting] Clean-Up ThreadsafeTimer methods

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/threading/ThreadsafeTimers.java
bundles/org.openhab.automation.jsscripting/src/main/resources/node_modules/@jsscripting-globals.js

index 2f45a650112c90ab3184edef5fbeaa8fd8bf4019..d82686e20c81b035de8d857a20c4b8496752cdbf 100644 (file)
@@ -100,20 +100,6 @@ public class ThreadsafeTimers {
      *         <code>clearTimeout()</code> to cancel the timeout.
      */
     public long setTimeout(Runnable callback, Long delay) {
-        return setTimeout(callback, delay, new Object());
-    }
-
-    /**
-     * <a href="https://developer.mozilla.org/en-US/docs/Web/API/setTimeout"><code>setTimeout()</code></a> polyfill.
-     * Sets a timer which executes a given function once the timer expires.
-     *
-     * @param callback function to run after the given delay
-     * @param delay time in milliseconds that the timer should wait before the callback is executed
-     * @param args
-     * @return Positive integer value which identifies the timer created; this value can be passed to
-     *         <code>clearTimeout()</code> to cancel the timeout.
-     */
-    public long setTimeout(Runnable callback, Long delay, @Nullable Object... args) {
         long id = lastId.incrementAndGet();
         ScheduledCompletableFuture<Object> future = scheduler.schedule(() -> {
             lock.lock();
@@ -153,20 +139,6 @@ public class ThreadsafeTimers {
      *         <code>clearInterval()</code> to cancel the interval.
      */
     public long setInterval(Runnable callback, Long delay) {
-        return setInterval(callback, delay, new Object());
-    }
-
-    /**
-     * <a href="https://developer.mozilla.org/en-US/docs/Web/API/setInterval"><code>setInterval()</code></a> polyfill.
-     * Repeatedly calls a function with a fixed time delay between each call.
-     *
-     * @param callback function to run
-     * @param delay time in milliseconds that the timer should delay in between executions of the callback
-     * @param args
-     * @return Numeric, non-zero value which identifies the timer created; this value can be passed to
-     *         <code>clearInterval()</code> to cancel the interval.
-     */
-    public long setInterval(Runnable callback, Long delay, @Nullable Object... args) {
         long id = lastId.incrementAndGet();
         ScheduledCompletableFuture<Object> future = scheduler.schedule(() -> {
             lock.lock();
index 444955f0d0cbcc4f66132a9d863632d99e9c4f8e..53750484d19ac943f61c71438a211154ce9b1a9e 100644 (file)
 
   // Polyfill common NodeJS functions onto the global object
   globalThis.console = console;
-  globalThis.setTimeout = ThreadsafeTimers.setTimeout;
+  globalThis.setTimeout = function (functionRef, delay, ...args) {
+    ThreadsafeTimers.setTimeout(() => functionRef(...args), delay);
+  };
   globalThis.clearTimeout = ThreadsafeTimers.clearTimeout;
-  globalThis.setInterval = ThreadsafeTimers.setInterval;
+  globalThis.setInterval = function (functionRef, delay, ...args) {
+    ThreadsafeTimers.setInterval(() => functionRef(...args), delay);
+  };
   globalThis.clearInterval = ThreadsafeTimers.clearInterval;
 
   // Support legacy NodeJS libraries