use napi::{ bindgen_prelude::{ClassInstance, Function, FunctionRef}, Env, JsFunction, JsObject, Result, }; use crate::class::Animal; #[napi] pub fn call0(callback: JsFunction) -> Result { callback.call0() } #[napi] pub fn call1(callback: JsFunction, arg: u32) -> Result { callback.call1(arg) } #[napi] pub fn call2(callback: JsFunction, arg1: u32, arg2: u32) -> Result { callback.call2(arg1, arg2) } #[napi] pub fn apply0(ctx: ClassInstance, callback: JsFunction) -> Result<()> { callback.apply0(ctx) } #[napi] pub fn apply1(ctx: ClassInstance, callback: JsFunction, name: String) -> Result<()> { callback.apply1(ctx, name) } #[napi] pub fn call_function(cb: Function<(), u32>) -> Result { cb.call(()) } #[napi] pub fn call_function_with_arg(cb: Function<(u32, u32), u32>, arg0: u32, arg1: u32) -> Result { cb.call((arg0, arg1)) } #[napi(ts_return_type = "Promise")] pub fn create_reference_on_function(env: Env, cb: Function<(), ()>) -> Result { let reference = cb.create_ref()?; env.execute_tokio_future( async { tokio::time::sleep(std::time::Duration::from_millis(100)).await; Ok(()) }, move |env, _| { let cb = reference.borrow_back(env)?; cb.call(())?; Ok(()) }, ) } #[napi] pub fn call_function_with_arg_and_ctx( ctx: ClassInstance, cb: Function, name: String, ) -> Result<()> { cb.apply(ctx, name) } #[napi] pub fn reference_as_callback( env: Env, callback: FunctionRef<(u32, u32), u32>, arg0: u32, arg1: u32, ) -> Result { callback.borrow_back(&env)?.call((arg0, arg1)) }