Skip to content
File

Blob: src/workerd/io/io-timers.c++

955 bytes
1#include "io-timers.h"
2 
3namespace workerd {
4 
5TimeoutId TimeoutId::Generator::getNext() {
6 // The maximum integer value that we can represent as a double to convey to jsg.
7 constexpr ValueType MAX_SAFE_INTEGER = (1ull << 53) - 1;
8 
9 auto id = nextId++;
10 if (nextId > MAX_SAFE_INTEGER) {
11 KJ_LOG(WARNING, "Unable to set timeout because there are no more unique ids");
12 JSG_FAIL_REQUIRE(Error,
13 "Unable to set timeout because there are no more unique ids "
14 "less than Number.MAX_SAFE_INTEGER.");
15 }
16 return TimeoutId(id);
17}
18 
19TimeoutManager::TimeoutParameters::TimeoutParameters(
20 bool repeat, int64_t msDelay, jsg::Function<void()> function)
21 : repeat(repeat),
22 msDelay(msDelay),
23 function(kj::mv(function)) {
24 // Don't allow pushing Date.now() backwards! This should be checked before TimeoutParameters
25 // is created but just in case...
26 if (msDelay < 0) {
27 this->msDelay = 0;
28 }
29}
30 
31} // namespace workerd