[jsscripting] ThreadsafeTimers: Add overrides for double delay for setTimeout & setInterval (#17148)

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Florian Hotze 2024-07-25 23:10:27 +02:00 committed by Ciprian Pascu
parent c8ffe3763a
commit 3808984572

View 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.