napi-rs/examples/napi/src/task.rs

32 lines
683 B
Rust
Raw Normal View History

2021-11-02 21:36:34 +09:00
use std::thread::sleep;
use napi::bindgen_prelude::*;
use napi::Task;
struct DelaySum(u32, u32);
#[napi(task)]
2021-11-02 21:36:34 +09:00
impl Task for DelaySum {
type Output = u32;
type JsValue = u32;
fn compute(&mut self) -> Result<Self::Output> {
sleep(std::time::Duration::from_millis(100));
2021-11-02 21:36:34 +09:00
Ok(self.0 + self.1)
}
fn resolve(&mut self, _env: napi::Env, output: Self::Output) -> Result<Self::JsValue> {
2021-11-02 21:36:34 +09:00
Ok(output)
}
}
#[napi]
fn without_abort_controller(a: u32, b: u32) -> AsyncTask<DelaySum> {
AsyncTask::new(DelaySum(a, b))
}
#[napi]
fn with_abort_controller(a: u32, b: u32, signal: AbortSignal) -> AsyncTask<DelaySum> {
AsyncTask::with_signal(DelaySum(a, b), signal)
2021-11-02 21:36:34 +09:00
}