diff options
author | Matt Mastracci <matthew@mastracci.com> | 2024-02-16 08:35:51 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-16 15:35:51 +0000 |
commit | f705906256f4a4d4ec262db64322c9fc3e5beb81 (patch) | |
tree | 1138630ebc1b10a2d0d71343568fa13f146bbfbe /ext/web/timers.rs | |
parent | e06934fe77cb7991fc2c767f221577a2e96506a7 (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/timers.rs')
-rw-r--r-- | ext/web/timers.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/ext/web/timers.rs b/ext/web/timers.rs index f7a0167df..f01dcb860 100644 --- a/ext/web/timers.rs +++ b/ext/web/timers.rs @@ -75,6 +75,16 @@ pub fn op_timer_handle(state: &mut OpState) -> ResourceId { .add(TimerHandle(CancelHandle::new_rc())) } +/// Bifurcate the op_sleep op into an interval one we can use for sanitization purposes. +#[op2(async(lazy), fast)] +pub async fn op_sleep_interval( + state: Rc<RefCell<OpState>>, + #[smi] millis: u64, + #[smi] rid: ResourceId, +) -> Result<bool, AnyError> { + op_sleep::call(state, millis, rid).await +} + /// Waits asynchronously until either `millis` milliseconds have passed or the /// [`TimerHandle`] resource given by `rid` has been canceled. /// @@ -85,6 +95,13 @@ pub async fn op_sleep( #[smi] millis: u64, #[smi] rid: ResourceId, ) -> Result<bool, AnyError> { + // If this timeout is scheduled for 0ms it means we want it to run at the + // end of the event loop turn. Since this is a lazy op, we can just return + // having already spun the event loop. + if millis == 0 { + return Ok(true); + } + // If the timer is not present in the resource table it was cancelled before // this op was polled. let Ok(handle) = state.borrow().resource_table.get::<TimerHandle>(rid) else { |