From 879a8d2100b4b27afc6e3e979821a60fc945cf28 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Tue, 28 May 2019 19:54:12 -0500 Subject: [PATCH] DEV: for the repl use case allow changed file reimports. Closes #47 --- src/build/assets.rs | 25 ++++++++++++++++++++++--- src/build/mod.rs | 26 ++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/build/assets.rs b/src/build/assets.rs index 94fa790..ffaa78b 100644 --- a/src/build/assets.rs +++ b/src/build/assets.rs @@ -34,17 +34,21 @@ pub type Result = result::Result; pub trait Cache { fn has_path(&self, path: &PathBuf) -> Result; fn get(&self, path: &PathBuf) -> Result>>; - fn stash(&mut self, path: PathBuf, asset: Rc) -> Result<()>; + fn stash(&mut self, path: PathBuf, asset: Rc, modkey: u64) -> Result<()>; + fn evict(&mut self, path: &PathBuf) -> Result<()>; + fn check_mod_key(&self, path: &PathBuf, modkey: u64) -> Result; } pub struct MemoryCache { map: HashMap>, + mod_key_map: HashMap, } impl MemoryCache { pub fn new() -> Self { MemoryCache { map: HashMap::new(), + mod_key_map: HashMap::new(), } } } @@ -58,8 +62,23 @@ impl Cache for MemoryCache { Ok(self.map.get(path).map(|v| v.clone())) } - fn stash(&mut self, path: PathBuf, asset: Rc) -> Result<()> { - self.map.insert(path, asset); + fn stash(&mut self, path: PathBuf, asset: Rc, modkey: u64) -> Result<()> { + self.map.insert(path.clone(), asset); + self.mod_key_map.insert(path, modkey); Ok(()) } + + fn evict(&mut self, path: &PathBuf) -> Result<()> { + self.map.remove(path); + self.mod_key_map.remove(path); + Ok(()) + } + + fn check_mod_key(&self, path: &PathBuf, modkey: u64) -> Result { + Ok(self + .mod_key_map + .get(path) + .map(|v| *v == modkey) + .unwrap_or(true)) + } } diff --git a/src/build/mod.rs b/src/build/mod.rs index 29d651d..92c2da7 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -415,7 +415,9 @@ where } }; let mut mut_assets_cache = self.assets.borrow_mut(); - mut_assets_cache.stash(path, result.clone())?; + // standard library assets are not real files so the + // mod key is always 0. + mut_assets_cache.stash(path, result.clone(), 0)?; return Ok(result); } else { return Err(error::BuildError::with_pos( @@ -453,8 +455,24 @@ where ) .to_boxed()); } - // Introduce a scope so the above borrow is dropped before we modify - // the cache below. + // 1. calculate mod time for file. + let lib_meta = std::fs::metadata(&normalized)?; + let modkey = lib_meta + .modified()? + .duration_since(std::time::SystemTime::UNIX_EPOCH)? + .as_secs(); + { + eprintln!("{} modkey...", modkey); + // 2. check mod time against previous stored mod time for file. + if !self.assets.borrow().check_mod_key(&normalized, modkey)? { + // 3. if different then evict from the cache + eprintln!( + "{} has changed evicting from cache...", + normalized.to_string_lossy().to_string() + ); + self.assets.borrow_mut().evict(&normalized)?; + } + } // Only parse the file once on import. let maybe_asset = self.assets.borrow().get(&normalized)?; let result = match maybe_asset { @@ -480,7 +498,7 @@ where } }; let mut mut_assets_cache = self.assets.borrow_mut(); - mut_assets_cache.stash(normalized.clone(), result.clone())?; + mut_assets_cache.stash(normalized.clone(), result.clone(), modkey)?; return Ok(result); }