1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
use crate::*; pub fn spawn<F: FnOnce() + Send + 'static>(task: F) { #[cfg(target_arch = "wasm32")] { static QUEUE: once_cell::sync::Lazy< Mutex<std::collections::VecDeque<Box<dyn FnOnce() + Send>>>, > = once_cell::sync::Lazy::new(|| todo!()); fn callback() { let timer = Timer::new(); while let Some(task) = QUEUE.lock().unwrap().pop_front() { task(); if timer.elapsed() > 0.001 { break; } } } QUEUE.lock().unwrap().push_back(Box::new(task)); } #[cfg(not(target_arch = "wasm32"))] global_threadpool().execute(task); }