From 9ae9adf6f6198099ecad982b453b51d9d6a3b72b Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Thu, 25 Jul 2024 23:10:27 +0200 Subject: [PATCH] [jsscripting] ThreadsafeTimers: Add overrides for double delay for setTimeout & setInterval (#17148) Signed-off-by: Florian Hotze --- .../internal/threading/ThreadsafeTimers.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/threading/ThreadsafeTimers.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/threading/ThreadsafeTimers.java index a73096d1c7..1d7ea89e59 100644 --- a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/threading/ThreadsafeTimers.java +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/threading/ThreadsafeTimers.java @@ -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> 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 * clearTimeout() to cancel the timeout. */ - public long setTimeout(Runnable callback, Long delay) { + public long setTimeout(Runnable callback, long delay) { long id = lastId.incrementAndGet(); ScheduledCompletableFuture 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)); + } + /** * clearTimeout() polyfill. * Cancels a timeout previously created by setTimeout(). @@ -138,7 +142,7 @@ public class ThreadsafeTimers { * @return Numeric, non-zero value which identifies the timer created; this value can be passed to * clearInterval() to cancel the interval. */ - public long setInterval(Runnable callback, Long delay) { + public long setInterval(Runnable callback, long delay) { long id = lastId.incrementAndGet(); ScheduledCompletableFuture 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)); + } + /** * clearInterval() * polyfill. -- 2.47.3