Skip to content

Commit cbcc23e

Browse files
committed
add docs
1 parent be6ef8a commit cbcc23e

2 files changed

Lines changed: 71 additions & 19 deletions

File tree

notify-debouncer-mini/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# Notify debouncer
22

3+
[![» Docs](https://flat.badgen.net/badge/api/docs.rs/df3600)][docs]
4+
35
Tiny debouncer for notify. Filters incoming events and emits only one event per timeframe per file.
46

57
## Features
68

79
- `crossbeam` enabled by default, for crossbeam channel support.
810
This may create problems used in tokio environments. See [#380](https://github.com/notify-rs/notify/issues/380).
911
Use someting like `notify-debouncer-mini = { version = "*", default-features = false }` to disable it.
10-
- `serde` for serde support of event types, off by default
12+
- `serde` for serde support of event types, off by default
13+
14+
[docs]: https://docs.rs/notify/0.1/notify-debouncer-mini/

notify-debouncer-mini/src/lib.rs

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
1-
//! Debouncer & access code
1+
//! Debouncer for notify
2+
//!
3+
//! # Installation
4+
//!
5+
//! ```toml
6+
//! [dependencies]
7+
//! notify = "5.0.0-pre.15"
8+
//! notify-debouncer-mini = "0.1"
9+
//! ```
10+
//!
11+
//! # Examples
12+
//!
13+
//! ```rust,no_run
14+
//! # use std::path::Path;
15+
//! # use std::time::Duration;
16+
//! use notify::{Watcher, RecursiveMode, Result};
17+
//! use notify_debouncer_mini::{new_debouncer,DebounceEventResult};
18+
//!
19+
//! # fn main() {
20+
//! // Select recommended watcher for debouncer.
21+
//! // Using a callback here, could also be a channel.
22+
//! let mut debouncer = new_debouncer(Duration::from_secs(2), None, |res: DebounceEventResult| {
23+
//! match res {
24+
//! Ok(events) => events.iter().for_each(|e|println!("Event {:?} for {:?}",e.kind,e.path)),
25+
//! Err(errors) => errors.iter().for_each(|e|println!("Error {:?}",e)),
26+
//! }
27+
//! }).unwrap();
28+
//!
29+
//! // Add a path to be watched. All files and directories at that path and
30+
//! // below will be monitored for changes.
31+
//! debouncer.watcher().watch(Path::new("."), RecursiveMode::Recursive).unwrap();
32+
//! # }
33+
//! ```
34+
//!
35+
//! # Features
36+
//!
37+
//! The following feature can be turned on or off.
38+
//!
39+
//! - `crossbeam-channel` enabled by default, adds DebounceEventHandler support for crossbeam channels.
40+
//! - `serde` enabled serde support for events.
241
#[cfg(feature = "serde")]
342
use serde::{Deserialize, Serialize};
443
use std::{
@@ -17,43 +56,50 @@ use notify::{Error, ErrorKind, Event, RecommendedWatcher, Watcher};
1756
///
1857
/// # Example implementation
1958
///
20-
/// ```no_run
21-
/// use notify::{Event, Result, EventHandler};
59+
/// ```rust,no_run
60+
/// # use notify::{Event, Result, EventHandler};
61+
/// # use notify_debouncer_mini::{DebounceEventHandler,DebounceEventResult};
2262
///
2363
/// /// Prints received events
2464
/// struct EventPrinter;
2565
///
26-
/// impl EventHandler for EventPrinter {
27-
/// fn handle_event(&mut self, event: Result<Event>) {
28-
/// if let Ok(event) = event {
29-
/// println!("Event: {:?}", event);
66+
/// impl DebounceEventHandler for EventPrinter {
67+
/// fn handle_event(&mut self, event: DebounceEventResult) {
68+
/// match event {
69+
/// Ok(events) => {
70+
/// for event in events {
71+
/// println!("Event {:?} for path {:?}",event.kind,event.path);
72+
/// }
73+
/// },
74+
/// // errors are batched, so you get either events or errors, probably both per debounce tick (two calls)
75+
/// Err(errors) => errors.iter().for_each(|e|println!("Got error {:?}",e)),
3076
/// }
3177
/// }
3278
/// }
3379
/// ```
3480
pub trait DebounceEventHandler: Send + 'static {
3581
/// Handles an event.
36-
fn handle_event(&mut self, event: DebouncedEvents);
82+
fn handle_event(&mut self, event: DebounceEventResult);
3783
}
3884

3985
impl<F> DebounceEventHandler for F
4086
where
41-
F: FnMut(DebouncedEvents) + Send + 'static,
87+
F: FnMut(DebounceEventResult) + Send + 'static,
4288
{
43-
fn handle_event(&mut self, event: DebouncedEvents) {
89+
fn handle_event(&mut self, event: DebounceEventResult) {
4490
(self)(event);
4591
}
4692
}
4793

4894
#[cfg(feature = "crossbeam")]
49-
impl DebounceEventHandler for crossbeam_channel::Sender<DebouncedEvents> {
50-
fn handle_event(&mut self, event: DebouncedEvents) {
95+
impl DebounceEventHandler for crossbeam_channel::Sender<DebounceEventResult> {
96+
fn handle_event(&mut self, event: DebounceEventResult) {
5197
let _ = self.send(event);
5298
}
5399
}
54100

55-
impl DebounceEventHandler for std::sync::mpsc::Sender<DebouncedEvents> {
56-
fn handle_event(&mut self, event: DebouncedEvents) {
101+
impl DebounceEventHandler for std::sync::mpsc::Sender<DebounceEventResult> {
102+
fn handle_event(&mut self, event: DebounceEventResult) {
57103
let _ = self.send(event);
58104
}
59105
}
@@ -76,14 +122,16 @@ impl EventData {
76122
}
77123
}
78124

79-
type DebouncedEvents = Result<Vec<DebouncedEvent>, Vec<Error>>;
125+
/// A result of debounced events.
126+
/// Comes with either a vec of events or vec of errors.
127+
pub type DebounceEventResult = Result<Vec<DebouncedEvent>, Vec<Error>>;
80128

81129
/// A debounced event kind.
82130
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
83131
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
84132
#[non_exhaustive]
85133
pub enum DebouncedEventKind {
86-
/// When precise events are disabled for files
134+
/// No precise events
87135
Any,
88136
/// Event but debounce timed out (for example continuous writes)
89137
AnyContinuous,
@@ -204,7 +252,7 @@ impl<T: Watcher> Drop for Debouncer<T> {
204252

205253
/// Creates a new debounced watcher with custom configuration.
206254
///
207-
/// Timeout is the amount of time after which a debounced event is emitted or a Continuous event is send, if there still are events incoming for the specific path.
255+
/// Timeout is the amount of time after which a debounced event is emitted or a continuous event is send, if there still are events incoming for the specific path.
208256
///
209257
/// If tick_rate is None, notify will select a tick rate that is less than the provided timeout.
210258
pub fn new_debouncer_opt<F: DebounceEventHandler, T: Watcher>(
@@ -285,7 +333,7 @@ pub fn new_debouncer_opt<F: DebounceEventHandler, T: Watcher>(
285333

286334
/// Short function to create a new debounced watcher with the recommended debouncer.
287335
///
288-
/// Timeout is the amount of time after which a debounced event is emitted or a Continuous event is send, if there still are events incoming for the specific path.
336+
/// Timeout is the amount of time after which a debounced event is emitted or a continuous event is send, if there still are events incoming for the specific path.
289337
///
290338
/// If tick_rate is None, notify will select a tick rate that is less than the provided timeout.
291339
pub fn new_debouncer<F: DebounceEventHandler>(

0 commit comments

Comments
 (0)