]> git.basschouten.com Git - openhab-addons.git/commitdiff
[jsscripting] ThreadsafeTimers: Add overrides for double delay for setTimeout & setIn...
authorFlorian Hotze <florianh_dev@icloud.com>
Thu, 25 Jul 2024 21:10:27 +0000 (23:10 +0200)
committerGitHub <noreply@github.com>
Thu, 25 Jul 2024 21:10:27 +0000 (23:10 +0200)
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

index a73096d1c784c6c1cfe1ad22a36a71c67584f011..1d7ea89e5959492f737b20b5b57c9bcb6fc7aaf4 100644 (file)
@@ -42,7 +42,7 @@ public class ThreadsafeTimers {
     // Mapping of positive, non-zero integer values (used as timeoutID or intervalID) and the Scheduler
     private final Map<Long, ScheduledCompletableFuture<Object>> idSchedulerMapping = new ConcurrentHashMap<>();
     private AtomicLong lastId = new AtomicLong();
-    private String identifier = "noIdentifier";
+    private String identifier = "javascript";
 
     public ThreadsafeTimers(Lock lock, ScriptExecution scriptExecution, Scheduler scheduler) {
         this.lock = lock;
@@ -99,7 +99,7 @@ public class ThreadsafeTimers {
      * @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) {
+    public long setTimeout(Runnable callback, long delay) {
         long id = lastId.incrementAndGet();
         ScheduledCompletableFuture<Object> future = scheduler.schedule(() -> {
             lock.lock();
@@ -115,6 +115,10 @@ public class ThreadsafeTimers {
         return id;
     }
 
+    public long setTimeout(Runnable callback, double delay) {
+        return setTimeout(callback, Math.round(delay));
+    }
+
     /**
      * <a href="https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout"><code>clearTimeout()</code></a> polyfill.
      * Cancels a timeout previously created by <code>setTimeout()</code>.
@@ -138,7 +142,7 @@ public class ThreadsafeTimers {
      * @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) {
+    public long setInterval(Runnable callback, long delay) {
         long id = lastId.incrementAndGet();
         ScheduledCompletableFuture<Object> future = scheduler.schedule(() -> {
             lock.lock();
@@ -153,6 +157,10 @@ public class ThreadsafeTimers {
         return id;
     }
 
+    public long setInterval(Runnable callback, double delay) {
+        return setInterval(callback, Math.round(delay));
+    }
+
     /**
      * <a href="https://developer.mozilla.org/en-US/docs/Web/API/clearInterval"><code>clearInterval()</code></a>
      * polyfill.