mirror of
https://github.com/napi-rs/napi-rs.git
synced 2026-01-25 15:38:06 +00:00
* feat(cli): allow sync fs operation between workers/mainThread * allow sync fs operation between workers/mainThread (#2065) * Fix * Update fixture * flaky test * Fix cross compile target * Update zig * macos-cross test was filtered --------- Co-authored-by: Toyo Li <lifenglin314@outlook.com>
96 lines
2.0 KiB
Rust
96 lines
2.0 KiB
Rust
use std::thread::sleep;
|
|
|
|
use napi::bindgen_prelude::*;
|
|
|
|
struct DelaySum(u32, u32);
|
|
|
|
#[napi]
|
|
impl napi::Task for DelaySum {
|
|
type Output = u32;
|
|
type JsValue = u32;
|
|
|
|
fn compute(&mut self) -> Result<Self::Output> {
|
|
sleep(std::time::Duration::from_millis(100));
|
|
Ok(self.0 + self.1)
|
|
}
|
|
|
|
fn resolve(&mut self, _env: napi::Env, output: Self::Output) -> Result<Self::JsValue> {
|
|
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)
|
|
}
|
|
|
|
struct AsyncTaskVoidReturn {}
|
|
|
|
#[napi]
|
|
impl Task for AsyncTaskVoidReturn {
|
|
type JsValue = ();
|
|
type Output = ();
|
|
|
|
fn compute(&mut self) -> Result<Self::Output> {
|
|
Ok(())
|
|
}
|
|
|
|
fn resolve(&mut self, _env: Env, output: Self::Output) -> Result<Self::JsValue> {
|
|
Ok(output)
|
|
}
|
|
}
|
|
|
|
#[napi]
|
|
fn async_task_void_return() -> AsyncTask<AsyncTaskVoidReturn> {
|
|
AsyncTask::new(AsyncTaskVoidReturn {})
|
|
}
|
|
|
|
struct AsyncTaskOptionalReturn {}
|
|
|
|
#[napi]
|
|
impl Task for AsyncTaskOptionalReturn {
|
|
type JsValue = Option<u32>;
|
|
type Output = ();
|
|
|
|
fn compute(&mut self) -> Result<Self::Output> {
|
|
Ok(())
|
|
}
|
|
|
|
fn resolve(&mut self, _env: Env, _: Self::Output) -> Result<Self::JsValue> {
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
#[napi]
|
|
fn async_task_optional_return() -> AsyncTask<AsyncTaskOptionalReturn> {
|
|
AsyncTask::new(AsyncTaskOptionalReturn {})
|
|
}
|
|
|
|
pub struct AsyncTaskReadFile {
|
|
path: String,
|
|
}
|
|
|
|
#[napi]
|
|
impl Task for AsyncTaskReadFile {
|
|
type Output = Vec<u8>;
|
|
type JsValue = Buffer;
|
|
|
|
fn compute(&mut self) -> Result<Self::Output> {
|
|
std::fs::read(&self.path).map_err(|e| Error::new(Status::GenericFailure, format!("{}", e)))
|
|
}
|
|
|
|
fn resolve(&mut self, _: Env, output: Self::Output) -> Result<Self::JsValue> {
|
|
Ok(output.into())
|
|
}
|
|
}
|
|
|
|
#[napi]
|
|
pub fn async_task_read_file(path: String) -> AsyncTask<AsyncTaskReadFile> {
|
|
AsyncTask::new(AsyncTaskReadFile { path })
|
|
}
|