napi-rs/crates/napi/src/js_values/global.rs

55 lines
1.5 KiB
Rust
Raw Normal View History

2020-10-03 00:23:21 +09:00
use std::convert::TryInto;
use super::*;
use crate::Env;
pub struct JsGlobal(pub(crate) Value);
pub struct JsTimeout(pub(crate) Value);
impl JsGlobal {
pub fn set_interval(&self, handler: JsFunction, interval: f64) -> Result<JsTimeout> {
2021-11-15 19:56:06 +09:00
let func: JsFunction = self.get_named_property_unchecked("setInterval")?;
2020-10-03 00:23:21 +09:00
func
.call(
None,
&[
handler.into_unknown(),
unsafe { Env::from_raw(self.0.env) }
2020-10-03 00:23:21 +09:00
.create_double(interval)?
.into_unknown(),
],
)
.and_then(|ret| ret.try_into())
}
2020-10-03 01:05:43 +09:00
pub fn clear_interval(&self, timer: JsTimeout) -> Result<JsUndefined> {
2021-11-15 19:56:06 +09:00
let func: JsFunction = self.get_named_property_unchecked("clearInterval")?;
2020-10-03 01:05:43 +09:00
func
.call(None, &[timer.into_unknown()])
.and_then(|ret| ret.try_into())
}
pub fn set_timeout(&self, handler: JsFunction, interval: f64) -> Result<JsTimeout> {
2021-11-15 19:56:06 +09:00
let func: JsFunction = self.get_named_property_unchecked("setTimeout")?;
2020-10-03 01:05:43 +09:00
func
.call(
None,
&[
handler.into_unknown(),
unsafe { Env::from_raw(self.0.env) }
2020-10-03 01:05:43 +09:00
.create_double(interval)?
.into_unknown(),
],
)
.and_then(|ret| ret.try_into())
}
pub fn clear_timeout(&self, timer: JsTimeout) -> Result<JsUndefined> {
2021-11-15 19:56:06 +09:00
let func: JsFunction = self.get_named_property_unchecked("clearTimeout")?;
2020-10-03 01:05:43 +09:00
func
.call(None, &[timer.into_unknown()])
.and_then(|ret| ret.try_into())
}
2020-10-03 00:23:21 +09:00
}