2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-04-14 15:10:48 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
//! This module helps deno implement timers and performance APIs.
|
2021-04-14 15:10:48 -04:00
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
use deno_core::CancelFuture;
|
|
|
|
use deno_core::CancelHandle;
|
2021-04-14 15:10:48 -04:00
|
|
|
use deno_core::OpState;
|
2021-12-07 07:39:58 -05:00
|
|
|
use deno_core::Resource;
|
|
|
|
use deno_core::ResourceId;
|
|
|
|
use std::borrow::Cow;
|
2021-04-14 15:10:48 -04:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
pub trait TimersPermission {
|
|
|
|
fn allow_hrtime(&mut self) -> bool;
|
|
|
|
fn check_unstable(&self, state: &OpState, api_name: &'static str);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type StartTime = Instant;
|
|
|
|
|
|
|
|
// Returns a milliseconds and nanoseconds subsec
|
|
|
|
// since the start time of the deno runtime.
|
|
|
|
// If the High precision flag is not set, the
|
|
|
|
// nanoseconds are rounded on 2ms.
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-04-14 15:10:48 -04:00
|
|
|
pub fn op_now<TP>(
|
|
|
|
state: &mut OpState,
|
|
|
|
_argument: (),
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-14 15:10:48 -04:00
|
|
|
) -> Result<f64, AnyError>
|
|
|
|
where
|
|
|
|
TP: TimersPermission + 'static,
|
|
|
|
{
|
|
|
|
let start_time = state.borrow::<StartTime>();
|
|
|
|
let seconds = start_time.elapsed().as_secs();
|
|
|
|
let mut subsec_nanos = start_time.elapsed().subsec_nanos() as f64;
|
|
|
|
let reduced_time_precision = 2_000_000.0; // 2ms in nanoseconds
|
|
|
|
|
|
|
|
// If the permission is not enabled
|
|
|
|
// Round the nano result on 2 milliseconds
|
|
|
|
// see: https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp#Reduced_time_precision
|
|
|
|
if !state.borrow_mut::<TP>().allow_hrtime() {
|
|
|
|
subsec_nanos -= subsec_nanos % reduced_time_precision;
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = (seconds * 1_000) as f64 + (subsec_nanos / 1_000_000.0);
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
pub struct TimerHandle(Rc<CancelHandle>);
|
|
|
|
|
|
|
|
impl Resource for TimerHandle {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"timer".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.0.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a [`TimerHandle`] resource that can be used to cancel invocations of
|
|
|
|
/// [`op_sleep`].
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-12-07 07:39:58 -05:00
|
|
|
pub fn op_timer_handle(
|
|
|
|
state: &mut OpState,
|
|
|
|
_: (),
|
|
|
|
_: (),
|
|
|
|
) -> Result<ResourceId, AnyError> {
|
|
|
|
let rid = state
|
|
|
|
.resource_table
|
|
|
|
.add(TimerHandle(CancelHandle::new_rc()));
|
|
|
|
Ok(rid)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Waits asynchronously until either `millis` milliseconds have passed or the
|
|
|
|
/// [`TimerHandle`] resource given by `rid` has been canceled.
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-12-07 07:39:58 -05:00
|
|
|
pub async fn op_sleep(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
millis: u64,
|
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let handle = state.borrow().resource_table.get::<TimerHandle>(rid)?;
|
|
|
|
tokio::time::sleep(Duration::from_millis(millis))
|
|
|
|
.or_cancel(handle.0.clone())
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-04-14 15:10:48 -04:00
|
|
|
pub fn op_sleep_sync<TP>(
|
|
|
|
state: &mut OpState,
|
|
|
|
millis: u64,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-14 15:10:48 -04:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
TP: TimersPermission + 'static,
|
|
|
|
{
|
|
|
|
state.borrow::<TP>().check_unstable(state, "Deno.sleepSync");
|
2021-12-07 07:39:58 -05:00
|
|
|
std::thread::sleep(Duration::from_millis(millis));
|
2021-04-14 15:10:48 -04:00
|
|
|
Ok(())
|
|
|
|
}
|