Skip to content

Commit

Permalink
fix: live preview content not reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
Cubxity committed Jul 23, 2023
1 parent 7fecd0f commit c04226b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
20 changes: 18 additions & 2 deletions src-tauri/src/project/manager.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::ipc::{FSRefreshEvent, ProjectChangeEvent, ProjectModel};
use crate::project::{is_project_config_file, Project, ProjectConfig};
use log::{debug, error, info, trace};
use log::{debug, error, info, trace, warn};
use notify::event::ModifyKind;
use notify::{Config, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use std::collections::HashMap;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, RwLock};
use tauri::{Runtime, Window};
use tokio::sync::mpsc::channel;
Expand Down Expand Up @@ -133,6 +133,7 @@ impl<R: Runtime> ProjectManager<R> {
kind
);
match kind {
// Refreshes the explorer view
FSHandleKind::Refresh => {
if let Ok(relative) = path.strip_prefix(&project.root) {
let event = FSRefreshEvent {
Expand All @@ -141,6 +142,7 @@ impl<R: Runtime> ProjectManager<R> {
let _ = window.emit("fs_refresh", &event);
}
}
// Reloads the file content, eg. project config or project source files
FSHandleKind::Reload => {
if let Ok(relative) = path.strip_prefix(&project.root) {
if is_project_config_file(relative) {
Expand All @@ -150,6 +152,20 @@ impl<R: Runtime> ProjectManager<R> {
*config_write = config;
config_write.apply(project);
}
} else {
let mut world = project.world.lock().unwrap();
let path = Path::new("/").join(relative);
match world.slot_update(&path, None) {
Ok(id) => {
debug!("updated slot for {:?} {:?} in {:?}", path, id, project);
}
Err(e) => {
warn!(
"unable to update slot for {:?} in {:?}: {:?}",
path, project, e
);
}
}
}
}
}
Expand Down
44 changes: 41 additions & 3 deletions src-tauri/src/project/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,26 @@ impl ProjectWorld {
let id = FileId::new(None, path.as_ref());
let mut slot = self.slot(id)?;

match slot.buffer.get_mut() {
// Only update existing buffers. There is no need to insert new buffers
Some(res) => {
// TODO: Avoid cloning?
let bytes = self.take_or_read_bytes(&path, content.clone())?;
match res {
Ok(b) => {
*b = bytes;
}
Err(_) => {
*res = Ok(bytes);
}
}
}
None => {}
};
match slot.source.get_mut() {
// Only update existing sources. There is no need to insert new sources
Some(res) => {
let content = ProjectWorld::take_or_read(&path, content)?;
let content = self.take_or_read(&path, content)?;
match res {
Ok(src) => {
// TODO: incremental edits
Expand Down Expand Up @@ -99,12 +115,34 @@ impl ProjectWorld {
}))
}

fn take_or_read<P: AsRef<Path>>(path: P, content: Option<String>) -> FileResult<String> {
fn take_or_read<P: AsRef<Path>>(&self, path: P, content: Option<String>) -> FileResult<String> {
if let Some(content) = content {
return Ok(content);
}

fs::read_to_string(path.as_ref()).map_err(|e| FileError::from_io(e, path.as_ref()))
let path = self
.root
.join_rooted(path.as_ref())
.ok_or(FileError::AccessDenied)?;
fs::read_to_string(&path).map_err(|e| FileError::from_io(e, &path))
}

fn take_or_read_bytes<P: AsRef<Path>>(
&self,
path: P,
content: Option<String>,
) -> FileResult<Bytes> {
if let Some(content) = content {
return Ok(Bytes::from(content.into_bytes()));
}

let path = self
.root
.join_rooted(path.as_ref())
.ok_or(FileError::AccessDenied)?;
fs::read(&path)
.map_err(|e| FileError::from_io(e, &path))
.map(Bytes::from)
}
}

Expand Down

0 comments on commit c04226b

Please sign in to comment.