napi-rs/examples/napi-compat-mode/src/global.rs
LongYinan 4719caa643
feat(napi): support Return generic of ThreadsafeFunction (#1997)
* feat(napi): support to use tuple with either (#1993)

`Either` uses `ValidateNapiValue` + `TypeName` to validate and report error on value not being matched. So there's no way to remove these super traits from it. So I implemented these types to `Tuple` types.

* feat(napi): support `Return` generic of ThreadsafeFunction

* depracate JsFunction

* CalleeHandled tsfn should handle Result in callback

* Pass env to call_with_return_value callback

* Fix compile

* clippy fix

* Fix electron test

* Function args

---------

Co-authored-by: Hana <andywangsy@gmail.com>
2024-03-20 21:37:08 +08:00

25 lines
696 B
Rust

use std::convert::TryInto;
use napi::{CallContext, JsNumber, JsObject, JsTimeout, JsUndefined, Result};
#[js_function(2)]
pub fn set_timeout(ctx: CallContext) -> Result<JsTimeout> {
let handler = ctx.get(0)?;
let timeout: JsNumber = ctx.get(1)?;
ctx
.env
.get_global()?
.set_timeout(handler, timeout.try_into()?)
}
#[js_function(1)]
pub fn clear_timeout(ctx: CallContext) -> Result<JsUndefined> {
let timer: JsTimeout = ctx.get(0)?;
ctx.env.get_global()?.clear_timeout(timer)
}
pub fn register_js(exports: &mut JsObject) -> Result<()> {
exports.create_named_method("setTimeout", set_timeout)?;
exports.create_named_method("clearTimeout", clear_timeout)?;
Ok(())
}