summaryrefslogtreecommitdiff
path: root/ext/web/02_timers.js
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2024-02-16 08:35:51 -0700
committerGitHub <noreply@github.com>2024-02-16 15:35:51 +0000
commitf705906256f4a4d4ec262db64322c9fc3e5beb81 (patch)
tree1138630ebc1b10a2d0d71343568fa13f146bbfbe /ext/web/02_timers.js
parente06934fe77cb7991fc2c767f221577a2e96506a7 (diff)
chore(ext/web): refactor timer ops before landing op sanitizer (#22435)
Splitting the sleep and interval ops allows us to detect an interval timer. We also remove the use of the `op_async_void_deferred` call. A future PR will be able to split the op sanitizer messages for timers and intervals.
Diffstat (limited to 'ext/web/02_timers.js')
-rw-r--r--ext/web/02_timers.js22
1 files changed, 9 insertions, 13 deletions
diff --git a/ext/web/02_timers.js b/ext/web/02_timers.js
index 2241e9614..8096c2ab5 100644
--- a/ext/web/02_timers.js
+++ b/ext/web/02_timers.js
@@ -4,8 +4,8 @@ import { core, primordials } from "ext:core/mod.js";
import {
op_now,
op_sleep,
+ op_sleep_interval,
op_timer_handle,
- op_void_async_deferred,
} from "ext:core/ops";
const {
ArrayPrototypePush,
@@ -193,6 +193,7 @@ function initializeTimer(
task,
timeout,
timerInfo,
+ repeat,
);
return id;
@@ -220,19 +221,13 @@ const scheduledTimers = { head: null, tail: null };
* after the timeout, if it hasn't been cancelled.
* @param {number} millis
* @param {{ cancelRid: number, isRef: boolean, promise: Promise<void> }} timerInfo
+ * @param {boolean} repeat
*/
-function runAfterTimeout(task, millis, timerInfo) {
+function runAfterTimeout(task, millis, timerInfo, repeat) {
const cancelRid = timerInfo.cancelRid;
- let sleepPromise;
- // If this timeout is scheduled for 0ms it means we want it to run at the
- // end of the event loop turn. There's no point in setting up a Tokio timer,
- // since its lowest resolution is 1ms. Firing of a "void async" op is better
- // in this case, because the timer will take closer to 0ms instead of >1ms.
- if (millis === 0) {
- sleepPromise = op_void_async_deferred();
- } else {
- sleepPromise = op_sleep(millis, cancelRid);
- }
+ const sleepPromise = repeat
+ ? op_sleep_interval(millis, cancelRid)
+ : op_sleep(millis, cancelRid);
timerInfo.promise = sleepPromise;
if (!timerInfo.isRef) {
core.unrefOpPromise(timerInfo.promise);
@@ -395,7 +390,8 @@ function unrefTimer(id) {
// for that reason: it lets promises make forward progress but can
// still starve other parts of the event loop.
function defer(go) {
- PromisePrototypeThen(op_void_async_deferred(), () => go());
+ // If we pass a delay of zero to op_sleep, it returns at the next event spin
+ PromisePrototypeThen(op_sleep(0, 0), () => go());
}
export {