summaryrefslogtreecommitdiff
path: root/ext/web/timers.rs
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2022-10-17 04:29:16 -0700
committerGitHub <noreply@github.com>2022-10-17 16:59:16 +0530
commit4c9dd33e27d31a0f6f8f26e043fbfbefa84db0f2 (patch)
tree43073429e2f8ca3d3bef2aa3bc0378ebf016a118 /ext/web/timers.rs
parente41af14b2a5e7643e4d4e882b20a828ef0104757 (diff)
perf(ext/web): optimize timer cancellation (#16316)
Towards #16315 It created a bunch of Error objects and rejected the promise. This patch changes `op_sleep` to resolve with `true` if it was cancelled.
Diffstat (limited to 'ext/web/timers.rs')
-rw-r--r--ext/web/timers.rs10
1 files changed, 6 insertions, 4 deletions
diff --git a/ext/web/timers.rs b/ext/web/timers.rs
index ba5e12d62..5b1310792 100644
--- a/ext/web/timers.rs
+++ b/ext/web/timers.rs
@@ -77,15 +77,17 @@ pub fn op_timer_handle(state: &mut OpState) -> ResourceId {
/// Waits asynchronously until either `millis` milliseconds have passed or the
/// [`TimerHandle`] resource given by `rid` has been canceled.
+///
+/// If the timer is canceled, this returns `false`. Otherwise, it returns `true`.
#[op(deferred)]
pub async fn op_sleep(
state: Rc<RefCell<OpState>>,
millis: u64,
rid: ResourceId,
-) -> Result<(), AnyError> {
+) -> Result<bool, AnyError> {
let handle = state.borrow().resource_table.get::<TimerHandle>(rid)?;
- tokio::time::sleep(Duration::from_millis(millis))
+ let res = tokio::time::sleep(Duration::from_millis(millis))
.or_cancel(handle.0.clone())
- .await?;
- Ok(())
+ .await;
+ Ok(res.is_ok())
}