2019-08-14 11:03:02 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 08:50:21 -04:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::state::ThreadSafeState;
|
|
|
|
use deno::*;
|
|
|
|
use futures::Future;
|
|
|
|
use std;
|
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
pub fn op_global_timer_stop(
|
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
_args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
2019-08-14 11:03:02 -04:00
|
|
|
let state = state;
|
|
|
|
let mut t = state.global_timer.lock().unwrap();
|
|
|
|
t.cancel();
|
2019-08-26 08:50:21 -04:00
|
|
|
Ok(JsonOp::Sync(json!({})))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct GlobalTimerArgs {
|
|
|
|
timeout: u64,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_global_timer(
|
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: GlobalTimerArgs = serde_json::from_value(args)?;
|
|
|
|
let val = args.timeout;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
|
|
|
let state = state;
|
|
|
|
let mut t = state.global_timer.lock().unwrap();
|
2019-09-19 18:09:43 -04:00
|
|
|
let deadline = Instant::now() + Duration::from_millis(val);
|
2019-08-26 08:50:21 -04:00
|
|
|
let f = t
|
|
|
|
.new_timeout(deadline)
|
|
|
|
.then(move |_| futures::future::ok(json!({})));
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
Ok(JsonOp::Async(Box::new(f)))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|