LongYinan 1e30088198
fix(wasm-runtime): avoid wasm-util from tree shaking by rollup (#2810)
* fix(wasm-runtime): avoid wasm-util from tree shaking by rollup

* fix filename
2025-08-07 12:55:48 +08:00

43 lines
1.0 KiB
Rust

use std::{thread, time::Duration};
use napi::{
threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode},
Error, Status,
};
pub fn worker(id: u32) {
println!("Worker {} started", id);
thread::sleep(Duration::from_millis(500));
thread::sleep(Duration::from_millis(200));
println!("Worker {} finished", id);
}
#[napi]
pub fn test_workers(amount: u32, complete_callback: ThreadsafeFunction<(), ()>) {
println!("Starting parallel workers...");
let mut handles = vec![];
for i in 0..amount {
let handle = thread::spawn(move || {
worker(i);
});
handles.push(handle);
}
thread::spawn(move || {
for handle in handles {
if let Err(e) = handle.join() {
complete_callback.call(
Err(Error::new(
Status::GenericFailure,
format!("Worker panicked {:?}", e),
)),
ThreadsafeFunctionCallMode::NonBlocking,
);
}
}
complete_callback.call(Ok(()), ThreadsafeFunctionCallMode::NonBlocking);
});
}