Fix config watching
This commit is contained in:
parent
1af2f4ffe5
commit
a85b369399
3 changed files with 29 additions and 36 deletions
|
|
@ -26,7 +26,6 @@ dirs.workspace = true
|
||||||
tokio = { version = "1.44", optional = true, features = ["time"] }
|
tokio = { version = "1.44", optional = true, features = ["time"] }
|
||||||
async-std = { version = "1.13", optional = true }
|
async-std = { version = "1.13", optional = true }
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
notify-debouncer-full = "0.5.0"
|
|
||||||
|
|
||||||
[target.'cfg(unix)'.dependencies]
|
[target.'cfg(unix)'.dependencies]
|
||||||
xdg = "2.5"
|
xdg = "2.5"
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ use notify::{
|
||||||
event::{EventKind, ModifyKind},
|
event::{EventKind, ModifyKind},
|
||||||
RecommendedWatcher, Watcher,
|
RecommendedWatcher, Watcher,
|
||||||
};
|
};
|
||||||
use notify_debouncer_full::{DebouncedEvent, Debouncer, RecommendedCache};
|
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
use std::{
|
use std::{
|
||||||
fmt, fs,
|
fmt, fs,
|
||||||
|
|
@ -246,7 +245,7 @@ impl Config {
|
||||||
// This may end up being an mpsc channel instead of a function
|
// This may end up being an mpsc channel instead of a function
|
||||||
// See EventHandler in the notify crate: https://docs.rs/notify/latest/notify/trait.EventHandler.html
|
// See EventHandler in the notify crate: https://docs.rs/notify/latest/notify/trait.EventHandler.html
|
||||||
// Having a callback allows for any application abstraction to be used
|
// Having a callback allows for any application abstraction to be used
|
||||||
pub fn watch<F>(&self, f: F) -> Result<Debouncer<RecommendedWatcher, RecommendedCache>, Error>
|
pub fn watch<F>(&self, f: F) -> Result<RecommendedWatcher, Error>
|
||||||
// Argument is an array of all keys that changed in that specific transaction
|
// Argument is an array of all keys that changed in that specific transaction
|
||||||
//TODO: simplify F requirements
|
//TODO: simplify F requirements
|
||||||
where
|
where
|
||||||
|
|
@ -257,51 +256,47 @@ impl Config {
|
||||||
return Err(Error::NoConfigDirectory);
|
return Err(Error::NoConfigDirectory);
|
||||||
};
|
};
|
||||||
let user_path_clone = user_path.clone();
|
let user_path_clone = user_path.clone();
|
||||||
let mut watcher = notify_debouncer_full::new_debouncer(
|
let mut watcher = notify::recommended_watcher(
|
||||||
Duration::from_secs(1),
|
move |event_res: Result<notify::Event, notify::Error>| {
|
||||||
None,
|
|
||||||
move |event_res: Result<Vec<DebouncedEvent>, Vec<notify::Error>>| {
|
|
||||||
match event_res {
|
match event_res {
|
||||||
Ok(events) => {
|
Ok(event) => {
|
||||||
for event in events {
|
match &event.kind {
|
||||||
match &event.event.kind {
|
EventKind::Access(_)
|
||||||
EventKind::Access(_)
|
| EventKind::Modify(ModifyKind::Metadata(_)) => {
|
||||||
| EventKind::Modify(ModifyKind::Metadata(_)) => {
|
// Data not mutated
|
||||||
// Data not mutated
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
let mut keys = Vec::new();
|
let mut keys = Vec::new();
|
||||||
for path in &event.paths {
|
for path in &event.paths {
|
||||||
match path.strip_prefix(&user_path_clone) {
|
match path.strip_prefix(&user_path_clone) {
|
||||||
Ok(key_path) => {
|
Ok(key_path) => {
|
||||||
if let Some(key) = key_path.to_str() {
|
if let Some(key) = key_path.to_str() {
|
||||||
// Skip any .atomicwrite temporary files
|
// Skip any .atomicwrite temporary files
|
||||||
if key.starts_with(".atomicwrite") {
|
if key.starts_with(".atomicwrite") {
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
keys.push(key.to_string());
|
|
||||||
}
|
}
|
||||||
}
|
keys.push(key.to_string());
|
||||||
Err(_err) => {
|
|
||||||
//TODO: handle errors
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
Err(_err) => {
|
||||||
if !keys.is_empty() {
|
//TODO: handle errors
|
||||||
f(&watch_config, &keys);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !keys.is_empty() {
|
||||||
|
f(&watch_config, &keys);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(_errs) => {
|
Err(_err) => {
|
||||||
//TODO: handle errors
|
//TODO: handle errors
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
watcher.watch(user_path, notify::RecursiveMode::NonRecursive)?;
|
watcher.watch(user_path, notify::RecursiveMode::Recursive)?;
|
||||||
Ok(watcher)
|
Ok(watcher)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
use iced_futures::futures::{SinkExt, Stream};
|
use iced_futures::futures::{SinkExt, Stream};
|
||||||
use iced_futures::{futures::channel::mpsc, stream};
|
use iced_futures::{futures::channel::mpsc, stream};
|
||||||
use notify::RecommendedWatcher;
|
use notify::RecommendedWatcher;
|
||||||
use notify_debouncer_full::{Debouncer, RecommendedCache};
|
|
||||||
use std::{borrow::Cow, hash::Hash};
|
use std::{borrow::Cow, hash::Hash};
|
||||||
|
|
||||||
use crate::{Config, CosmicConfigEntry};
|
use crate::{Config, CosmicConfigEntry};
|
||||||
|
|
@ -10,7 +9,7 @@ pub enum ConfigState<T> {
|
||||||
Init(Cow<'static, str>, u64, bool),
|
Init(Cow<'static, str>, u64, bool),
|
||||||
Waiting(
|
Waiting(
|
||||||
T,
|
T,
|
||||||
Debouncer<RecommendedWatcher, RecommendedCache>,
|
RecommendedWatcher,
|
||||||
mpsc::Receiver<Vec<String>>,
|
mpsc::Receiver<Vec<String>>,
|
||||||
Config,
|
Config,
|
||||||
),
|
),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue