diff options
Diffstat (limited to 'src/file.rs')
| -rw-r--r-- | src/file.rs | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/src/file.rs b/src/file.rs index b8f6610..c2c834e 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,28 +1,30 @@ +use std::{ffi::OsStr, path::Path, sync::Arc, time::Instant}; +#[cfg(feature = "hot-reload")] use std::{ - ffi::OsStr, - path::Path, sync::{ - Arc, atomic::{AtomicBool, Ordering}, mpsc::{Receiver, Sender}, }, - time::{Duration, Instant, SystemTime}, + time::{Duration, SystemTime}, }; use rustc_hash::FxHashMap; use smol::{Task, block_on, unblock}; -use crate::script::{WasmRunner, wasm::WasmModule}; +use crate::script::wasm::{WasmModule, WasmRunner}; type FileTask = Task<std::io::Result<WasmModule>>; pub struct FileManager { + #[cfg(feature = "hot-reload")] watcher: Notifier, + #[cfg(feature = "hot-reload")] updated_files: Receiver<Box<Path>>, active_tasks: FxHashMap<Box<Path>, FileTask>, cache: FxHashMap<Box<Path>, Arc<std::io::Result<WasmModule>>>, } +#[cfg(feature = "hot-reload")] struct Notifier { paths: Arc<spin::Mutex<Vec<Box<Path>>>>, cancel_token: Arc<AtomicBool>, @@ -39,6 +41,7 @@ fn compile_data(path: &Path, data: &[u8], compiler: &WasmRunner) -> std::io::Res Err(std::io::Error::other("Unrecognized file extension")) } +#[cfg(feature = "hot-reload")] impl Notifier { pub fn new(channel: Sender<Box<Path>>, interval: Duration) -> Self { let paths = Arc::new(spin::Mutex::new(Vec::new())); @@ -84,12 +87,16 @@ impl Notifier { impl FileManager { pub fn new() -> Self { + #[cfg(feature = "hot-reload")] let (sender, updated_files) = std::sync::mpsc::channel(); - let watcher = Notifier::new(sender, Duration::from_secs(10)); + #[cfg(feature = "hot-reload")] + let watcher = Notifier::new(sender, Duration::from_secs(2)); Self { active_tasks: FxHashMap::default(), cache: FxHashMap::default(), + #[cfg(feature = "hot-reload")] watcher, + #[cfg(feature = "hot-reload")] updated_files, } } @@ -116,6 +123,7 @@ impl FileManager { self.cache.entry(path.clone()).insert_entry(Arc::new(data)); } + #[cfg(feature = "hot-reload")] self.watcher.watch(path.clone()); self.cache.get(&path).unwrap().clone() } @@ -130,10 +138,12 @@ impl FileManager { &compiler, ) }); + #[cfg(feature = "hot-reload")] self.watcher.watch(path.clone()); self.active_tasks.insert(path, task); } + #[cfg(feature = "hot-reload")] pub fn reload_files(&mut self, wasm_compiler: &WasmRunner) { while let Ok(path) = self.updated_files.try_recv() { log::info!("reloading {}", path.to_string_lossy()); @@ -169,6 +179,7 @@ impl Default for FileManager { } } +#[cfg(feature = "hot-reload")] impl Drop for Notifier { fn drop(&mut self) { self.cancel_token.store(true, Ordering::Release); |
