aboutsummaryrefslogtreecommitdiff
path: root/src/watcher.rs
diff options
context:
space:
mode:
authorLibravatar Silas Bartha <silas@exvacuum.dev>2024-10-16 15:12:15 -0400
committerLibravatar Silas Bartha <silas@exvacuum.dev>2024-10-16 20:19:07 -0400
commit99c398cc127dbc83480f98fea8c76f7c19d4dce8 (patch)
treec4c311300ad4194217eb55b5f4c278694f6bcdc2 /src/watcher.rs
parent4fc097045b0baf8a3a5626681149f4540f8305d7 (diff)
Navigation Rewrite
Diffstat (limited to 'src/watcher.rs')
-rw-r--r--src/watcher.rs95
1 files changed, 27 insertions, 68 deletions
diff --git a/src/watcher.rs b/src/watcher.rs
index b94c0d4..78d74f2 100644
--- a/src/watcher.rs
+++ b/src/watcher.rs
@@ -1,4 +1,7 @@
-use std::{path::{Path, PathBuf}, time::Duration};
+use std::{
+ path::{Path, PathBuf},
+ time::Duration,
+};
use async_channel::{Receiver, Sender};
use bevy::{prelude::*, tasks::IoTaskPool};
@@ -9,16 +12,15 @@ use notify::{
use notify_debouncer_full::{new_debouncer, DebounceEventResult};
use crate::{
- commands::process_entry,
components::DirworldEntity,
- resources::{DirworldCodecs, DirworldObservers, DirworldRootDir},
+ resources::{DirworldCache, DirworldCodecs, DirworldObservers, DirworldRootDir},
};
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
pub struct DirworldWatcherSet;
/// Event fired when a file watcher event is caught.
-#[derive(Event)]
+#[derive(Event, Debug)]
pub struct DirworldWatcherEvent(pub notify::Event);
#[derive(Resource)]
@@ -42,23 +44,33 @@ pub fn setup(mut commands: Commands) {
async fn file_watcher(rx: Receiver<PathBuf>, tx: Sender<notify::Event>) {
let (watcher_tx, watcher_rx) = std::sync::mpsc::channel();
- let mut debouncer = new_debouncer(Duration::from_millis(500), None, move |result: DebounceEventResult| {
- match result {
- Ok(events) => for event in events.iter() {
- watcher_tx.send(event.clone()).unwrap();
+ let mut debouncer = new_debouncer(
+ Duration::from_millis(500),
+ None,
+ move |result: DebounceEventResult| match result {
+ Ok(events) => {
+ for event in events.iter() {
+ watcher_tx.send(event.clone()).unwrap();
+ }
}
- Err(errors) => for error in errors.iter() {
- error!("{error:?}");
+ Err(errors) => {
+ for error in errors.iter() {
+ error!("{error:?}");
+ }
}
- }
- }).unwrap();
+ },
+ )
+ .unwrap();
let mut old_path: Option<PathBuf> = None;
loop {
while let Ok(message) = rx.try_recv() {
if let Some(old_path) = &old_path {
debouncer.watcher().unwatch(old_path).unwrap();
}
- debouncer.watcher().watch(&message, RecursiveMode::NonRecursive).unwrap();
+ debouncer
+ .watcher()
+ .watch(&message, RecursiveMode::NonRecursive)
+ .unwrap();
old_path = Some(message);
}
@@ -70,8 +82,8 @@ async fn file_watcher(rx: Receiver<PathBuf>, tx: Sender<notify::Event>) {
pub fn update(
watcher_channels: Res<WatcherChannels>,
- mut event_writer: EventWriter<DirworldWatcherEvent>,
root_dir: Res<DirworldRootDir>,
+ mut commands: Commands,
) {
if root_dir.is_changed() {
if let Some(project_dir) = &root_dir.0 {
@@ -79,61 +91,8 @@ pub fn update(
}
} else {
while let Ok(event) = watcher_channels.rx_changes.try_recv() {
- event_writer.send(DirworldWatcherEvent(event));
+ commands.trigger(DirworldWatcherEvent(event));
}
}
}
-pub fn handle_changes(
- mut event_reader: EventReader<DirworldWatcherEvent>,
- mut commands: Commands,
- dirworld_entities: Query<(Entity, &DirworldEntity)>,
- observers: Res<DirworldObservers>,
- codecs: Res<DirworldCodecs>,
-) {
- if !event_reader.is_empty() {
- for DirworldWatcherEvent(event) in event_reader.read() {
- info!("Watcher Event: {event:?}");
- match event.kind {
- EventKind::Remove(_) | EventKind::Modify(ModifyKind::Name(RenameMode::From)) => {
- for path in &event.paths {
- remove_entity(&mut commands, &dirworld_entities, path);
- }
- }
- EventKind::Create(_) | EventKind::Modify(ModifyKind::Name(RenameMode::To)) => {
- for path in &event.paths {
- process_entry(&mut commands, path, &observers, &codecs);
- }
- }
- EventKind::Modify(ModifyKind::Name(RenameMode::Both))
- => {
- remove_entity(&mut commands, &dirworld_entities, &event.paths[0]);
- process_entry(&mut commands, &event.paths[1], &observers, &codecs);
- }
- // EventKind::Modify(ModifyKind::Data(DataChange::Content))
- EventKind::Modify(ModifyKind::Metadata(MetadataKind::Any)) => {
- remove_entity(&mut commands, &dirworld_entities, &event.paths[0]);
- process_entry(&mut commands, &event.paths[0], &observers, &codecs);
- }
- _ => {
- // warn!("Not Processed.")
- }
- }
- }
- }
-}
-
-fn remove_entity(
- commands: &mut Commands,
- dirworld_entities: &Query<(Entity, &DirworldEntity)>,
- path: &Path,
-) {
- if let Some((entity, _)) = dirworld_entities
- .iter()
- .find(|(_, dirworld_entity)| dirworld_entity.path == *path)
- {
- commands.entity(entity).despawn_recursive();
- } else {
- warn!("Failed to find entity corresponding to path for despawning: {path:?}");
- }
-}