Skip to content

Commit 8682510

Browse files
committed
replace old config and allow initialization configuration
This also allows correct configuration of PollWatcher via notify-debouncer-mini
1 parent ae9134c commit 8682510

File tree

17 files changed

+128
-114
lines changed

17 files changed

+128
-114
lines changed

examples/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ path = "monitor_raw.rs"
2121
name = "debounced"
2222
path = "debounced.rs"
2323

24+
[[example]]
25+
name = "debounced_custom"
26+
path = "debounced_full_custom.rs"
27+
2428
[[example]]
2529
name = "poll_sysfs"
2630
path = "poll_sysfs.rs"

examples/async_monitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use futures::{
22
channel::mpsc::{channel, Receiver},
33
SinkExt, StreamExt,
44
};
5-
use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher};
5+
use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher, Config};
66
use std::path::Path;
77

88
/// Async, futures channel based event watching
@@ -28,7 +28,7 @@ fn async_watcher() -> notify::Result<(RecommendedWatcher, Receiver<notify::Resul
2828
futures::executor::block_on(async {
2929
tx.send(res).await.unwrap();
3030
})
31-
})?;
31+
}, Config::default())?;
3232

3333
Ok((watcher, rx))
3434
}

examples/debounced.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{path::Path, time::Duration};
22

3-
use notify::{RecursiveMode, Watcher};
3+
use notify::{RecursiveMode};
44
use notify_debouncer_mini::new_debouncer;
55

66
/// Example for debouncer

examples/debounced_full_custom.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{path::Path, time::Duration};
22

3-
use notify::{RecursiveMode, Watcher};
4-
use notify_debouncer_mini::new_debouncer;
3+
use notify::{RecursiveMode, Config};
4+
use notify_debouncer_mini::new_debouncer_opt;
55

66
/// Debouncer with custom backend and waiting for exit
77
fn main() {
@@ -18,7 +18,7 @@ fn main() {
1818
// setup debouncer
1919
let (tx, rx) = std::sync::mpsc::channel();
2020
// select backend via fish operator, here PollWatcher backend
21-
let mut debouncer = new_debouncer_opt::<_,notify::PollWatcher>(Duration::from_secs(2), None, tx).unwrap();
21+
let mut debouncer = new_debouncer_opt::<_,notify::PollWatcher>(Duration::from_secs(2), None, tx, Config::default()).unwrap();
2222

2323
debouncer
2424
.watcher()

examples/hot_reload_tide/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async fn main() -> tide::Result<()> {
3838
Err(error) => println!("Error reloading config: {:?}", error),
3939
}
4040
}
41-
})?;
41+
},notify::Config::default())?;
4242

4343
watcher.watch(Path::new(CONFIG_PATH), RecursiveMode::Recursive)?;
4444

examples/monitor_raw.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
1+
use notify::{RecommendedWatcher, RecursiveMode, Watcher, Config};
22
use std::path::Path;
33

44
fn main() {
@@ -16,7 +16,7 @@ fn watch<P: AsRef<Path>>(path: P) -> notify::Result<()> {
1616

1717
// Automatically select the best implementation for your platform.
1818
// You can also access each implementation directly e.g. INotifyWatcher.
19-
let mut watcher = RecommendedWatcher::new(tx)?;
19+
let mut watcher = RecommendedWatcher::new(tx, Config::default())?;
2020

2121
// Add a path to be watched. All files and directories at that path and
2222
// below will be monitored for changes.

examples/poll_sysfs.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
/// This example can't be demonstrated under windows, it might be relevant for network shares
44
#[cfg(not(target_os = "windows"))]
55
fn not_windows_main() -> notify::Result<()> {
6-
use notify::poll::PollWatcherConfig;
7-
use notify::{PollWatcher, RecursiveMode, Watcher};
6+
use notify::{PollWatcher, RecursiveMode, Watcher, Config};
87
use std::path::Path;
98
use std::time::Duration;
109

@@ -27,13 +26,12 @@ fn not_windows_main() -> notify::Result<()> {
2726

2827
println!("watching {:?}...", paths);
2928
// configure pollwatcher backend
30-
let config = PollWatcherConfig {
31-
compare_contents: true, // crucial part for pseudo filesystems
32-
poll_interval: Duration::from_secs(2),
33-
};
29+
let config = Config::default()
30+
.with_compare_contents(true) // crucial part for pseudo filesystems
31+
.with_poll_interval(Duration::from_secs(2));
3432
let (tx, rx) = std::sync::mpsc::channel();
3533
// create pollwatcher backend
36-
let mut watcher = PollWatcher::with_config(tx, config)?;
34+
let mut watcher = PollWatcher::new(tx, config)?;
3735
for path in paths {
3836
// watch all paths
3937
watcher.watch(&path, RecursiveMode::Recursive)?;

examples/watcher_kind.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
use std::{path::Path, time::Duration};
2+
use notify::*;
23

3-
use notify::{poll::PollWatcherConfig, *};
4+
// exampale of detecting the recommended watcher kind
45
fn main() {
56
let (tx, rx) = std::sync::mpsc::channel();
7+
// This example is a little bit misleading as you can just create one Config and use it for all watchers.
8+
// That way the pollwatcher specific stuff is still configured, if it should be used.
69
let mut watcher: Box<dyn Watcher> = if RecommendedWatcher::kind() == WatcherKind::PollWatcher {
710
// custom config for PollWatcher kind
8-
let config = PollWatcherConfig {
9-
poll_interval: Duration::from_secs(1),
10-
..Default::default()
11-
};
12-
Box::new(PollWatcher::with_config(tx, config).unwrap())
11+
// you
12+
let config = Config::default()
13+
.with_poll_interval(Duration::from_secs(1));
14+
Box::new(PollWatcher::new(tx, config).unwrap())
1315
} else {
1416
// use default config for everything else
15-
Box::new(RecommendedWatcher::new(tx).unwrap())
17+
Box::new(RecommendedWatcher::new(tx, Config::default()).unwrap())
1618
};
1719

1820
// watch some stuff

notify-debouncer-mini/src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//!
55
//! ```toml
66
//! [dependencies]
7-
//! notify = "5.0.0-pre.15"
87
//! notify-debouncer-mini = "0.1"
98
//! ```
109
//!
@@ -13,10 +12,12 @@
1312
//! ```rust,no_run
1413
//! # use std::path::Path;
1514
//! # use std::time::Duration;
16-
//! use notify::{Watcher, RecursiveMode, Result};
17-
//! use notify_debouncer_mini::{new_debouncer,DebounceEventResult};
15+
//! use notify_debouncer_mini::{notify::*,new_debouncer,DebounceEventResult};
1816
//!
1917
//! # fn main() {
18+
//! // setup initial watcher backend config
19+
//! let config = Config::default();
20+
//!
2021
//! // Select recommended watcher for debouncer.
2122
//! // Using a callback here, could also be a channel.
2223
//! let mut debouncer = new_debouncer(Duration::from_secs(2), None, |res: DebounceEventResult| {
@@ -50,6 +51,7 @@ use std::{
5051
time::{Duration, Instant},
5152
};
5253

54+
pub use notify;
5355
use notify::{Error, ErrorKind, Event, RecommendedWatcher, Watcher};
5456

5557
/// The set of requirements for watcher debounce event handling functions.
@@ -259,6 +261,7 @@ pub fn new_debouncer_opt<F: DebounceEventHandler, T: Watcher>(
259261
timeout: Duration,
260262
tick_rate: Option<Duration>,
261263
mut event_handler: F,
264+
config: notify::Config
262265
) -> Result<Debouncer<T>, Error> {
263266
let data = DebounceData::default();
264267

@@ -320,7 +323,7 @@ pub fn new_debouncer_opt<F: DebounceEventHandler, T: Watcher>(
320323
// can't have multiple TX, so we need to pipe that through our debouncer
321324
Err(e) => lock.add_error(e),
322325
}
323-
})?;
326+
}, config)?;
324327

325328
let guard = Debouncer {
326329
watcher,
@@ -339,7 +342,7 @@ pub fn new_debouncer_opt<F: DebounceEventHandler, T: Watcher>(
339342
pub fn new_debouncer<F: DebounceEventHandler>(
340343
timeout: Duration,
341344
tick_rate: Option<Duration>,
342-
event_handler: F,
345+
event_handler: F
343346
) -> Result<Debouncer<RecommendedWatcher>, Error> {
344-
new_debouncer_opt::<F, RecommendedWatcher>(timeout, tick_rate, event_handler)
347+
new_debouncer_opt::<F, RecommendedWatcher>(timeout, tick_rate, event_handler, notify::Config::default())
345348
}

notify/src/config.rs

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,72 @@ impl RecursiveMode {
2121
}
2222
}
2323

24-
/// Runtime configuration items for watchers.
25-
///
26-
/// See the [`Watcher::configure`](../trait.Watcher.html#tymethod.configure) method for usage.
27-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
28-
pub enum Config {
29-
/// Enable or disable emitting precise event classification.
30-
///
31-
/// Applicable to all watchers.
32-
///
33-
/// When enabled, events are emitted with a `kind` set to as much precision about what kind of
34-
/// event they are as the backend is capable of providing. When disabled (default), events are
35-
/// instead emitted as `EventKind::Any`. `EventKind::Other` meta-events are left alone.
36-
PreciseEvents(bool),
37-
38-
/// Enable or disable emitting `Notice` events.
39-
///
40-
/// Applicable to debounced watchers only.
41-
///
42-
/// When enabled, the first modify or remove event for a path is emitted immediately with a
43-
/// [`Flag::Notice`](../event/enum.Flag.html) attribute within a debouncing period, enabling
44-
/// applications to respond more quickly.
45-
NoticeEvents(bool),
46-
47-
/// Enable or disable emitting `Ongoing` events.
48-
///
49-
/// Applicable to debounced watchers only.
50-
///
51-
/// When enabled, partial write events that are received after a `Modify(Data)` Notice but
52-
/// before the end of a debouncing period (and the emission of a `Modify(Data)` event) are
53-
/// passed through as `Modify(Data)` events with an `Ongoing` flag. These events are still
54-
/// debounced, but at a lower (configurable) interval than the debouncing interval.
55-
///
56-
/// To enable, provide `Some(Duration)`. To disable, provide `None`.
57-
///
58-
/// # Errors
59-
///
60-
/// - `InvalidConfigValue` if the interval provided is higher than the debounce interval.
61-
OngoingEvents(Option<Duration>),
24+
/// Watcher Backend configuration
25+
///
26+
/// This contains multiple settings that may relate to only one specific backend,
27+
/// such as to correctly configure each backend regardless of what is selected during runtime.
28+
///
29+
/// ```rust
30+
/// # use std::time::Duration;
31+
/// # use notify::Config;
32+
/// let config = Config::default()
33+
/// .with_poll_interval(Duration::from_secs(2))
34+
/// .with_compare_contents(true);
35+
/// ```
36+
///
37+
/// Some options can be changed during runtime, others have to be set when creating the watcher backend.
38+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
39+
pub struct Config {
40+
/// See [BackendConfig::with_poll_interval]
41+
poll_interval: Duration,
42+
43+
/// See [BackendConfig::with_compare_contents]
44+
compare_contents: bool,
6245
}
46+
47+
impl Config {
48+
/// For [crate::PollWatcher]
49+
///
50+
/// Interval between each rescan attempt. This can be extremely expensive for large
51+
/// file trees so it is recommended to measure and tune accordingly.
52+
///
53+
/// The default poll frequency is 30 seconds.
54+
pub fn with_poll_interval(mut self, dur: Duration) -> Self {
55+
self.poll_interval = dur;
56+
self
57+
}
58+
59+
/// Returns current setting
60+
pub fn poll_interval(&self) -> Duration {
61+
self.poll_interval
62+
}
63+
64+
/// For [crate::PollWatcher]
65+
///
66+
/// Optional feature that will evaluate the contents of changed files to determine if
67+
/// they have indeed changed using a fast hashing algorithm. This is especially important
68+
/// for pseudo filesystems like those on Linux under /sys and /proc which are not obligated
69+
/// to respect any other filesystem norms such as modification timestamps, file sizes, etc.
70+
/// By enabling this feature, performance will be significantly impacted as all files will
71+
/// need to be read and hashed at each `poll_interval`.
72+
///
73+
/// This can't be changed during runtime. Off by default.
74+
pub fn with_compare_contents(mut self, compare_contents: bool) -> Self {
75+
self.compare_contents = compare_contents;
76+
self
77+
}
78+
79+
/// Returns current setting
80+
pub fn compare_contents(&self) -> bool {
81+
self.compare_contents
82+
}
83+
}
84+
85+
impl Default for Config {
86+
fn default() -> Self {
87+
Self {
88+
poll_interval: Duration::from_secs(30),
89+
compare_contents: false
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)