2021-11-02 21:36:34 +09:00
|
|
|
use std::thread::sleep;
|
|
|
|
|
|
|
|
use napi::bindgen_prelude::*;
|
|
|
|
use napi::Task;
|
|
|
|
|
|
|
|
struct DelaySum(u32, u32);
|
|
|
|
|
2021-11-23 20:00:31 +09:00
|
|
|
#[napi]
|
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> {
|
2021-11-04 19:44:06 +09:00
|
|
|
sleep(std::time::Duration::from_millis(100));
|
2021-11-02 21:36:34 +09:00
|
|
|
Ok(self.0 + self.1)
|
|
|
|
}
|
|
|
|
|
2021-11-04 19:44:06 +09:00
|
|
|
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]
|
2021-11-04 19:44:06 +09:00
|
|
|
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
|
|
|
}
|