Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debouncer #286

Merged
merged 8 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ jobs:
if: matrix.version == '1.56.0' && matrix.os != 'macos-latest'
run: cargo check --features=serde

- name: check build example
- name: check build examples
if: matrix.version == 'stable'
run: cargo check -p watcher_kind
run: cargo check --package examples --examples

- name: test hot_reload_tide
if: matrix.version == 'stable'
run: cargo test -p hot_reload_tide
run: cargo test
working-directory: examples/hot_reload_tide

- name: test
if: matrix.version == 'stable'
Expand Down
75 changes: 11 additions & 64 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,69 +1,16 @@
[package]
name = "notify"
version = "5.0.0-pre.15"
rust-version = "1.56"
description = "Cross-platform filesystem notification library"
documentation = "https://docs.rs/notify"
homepage = "https://github.com/notify-rs/notify"
repository = "https://github.com/notify-rs/notify.git"
readme = "README.md"
license = "CC0-1.0 OR Artistic-2.0"
keywords = ["events", "filesystem", "notify", "watch"]
categories = ["filesystem"]
authors = [
"Félix Saparelli <me@passcod.name>",
"Daniel Faust <hessijames@gmail.com>"
]
[workspace]

edition = "2021"
exclude = [
"/clippy.toml",
".github/*"
members = [
"notify",
"notify-debouncer-mini",

# internal
"examples"
#"examples/hot_reload_tide" untill https://github.com/rustsec/rustsec/issues/501 is resolved
]

[dependencies]
bitflags = "1.0.4"
crossbeam-channel = "0.5.0"
filetime = "0.2.6"
libc = "0.2.4"
serde = { version = "1.0.89", features = ["derive"], optional = true }
walkdir = "2.0.1"

[target.'cfg(target_os="linux")'.dependencies]
inotify = { version = "0.9", default-features = false }
mio = { version = "0.8", features = ["os-ext"] }

[target.'cfg(target_os="macos")'.dependencies]
fsevent-sys = { version = "4", optional = true }
kqueue = { version = "1.0", optional = true }
mio = { version = "0.8", features = ["os-ext"], optional = true }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.8", features = ["fileapi", "handleapi", "ioapiset", "minwinbase", "synchapi", "winbase", "winnt"] }

[target.'cfg(any(target_os="freebsd", target_os="openbsd", target_os = "netbsd", target_os = "dragonflybsd"))'.dependencies]
kqueue = "^1.0.4" # fix for #344
mio = { version = "0.8", features = ["os-ext"] }

[dev-dependencies]
futures = "0.3"
serde_json = "1.0.39"
tempfile = "3.2.0"
nix = "0.23.1"

[features]
default = ["macos_fsevent"]
timing_tests = []
manual_tests = []
macos_kqueue = ["kqueue", "mio"]
macos_fsevent = ["fsevent-sys"]
exclude = ["examples/hot_reload_tide"]

[patch.crates-io]
notify = { path = "." }

[workspace]
members = [
".",
"examples/hot_reload_tide",
"examples/watcher_kind"
]
notify = { path = "notify/" }
notify-debouncer-mini = { path = "notify-debouncer-mini/" }
35 changes: 35 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "examples"
version = "0.0.0"
publish = false
edition = "2021"

[dev-dependencies]
notify = { version = "5.0.0-pre.15" }
notify-debouncer-mini = { version = "0.1" }
futures = "0.3"

[[example]]
name = "async_monitor"
path = "async_monitor.rs"

[[example]]
name = "monitor_raw"
path = "monitor_raw.rs"

[[example]]
name = "debounced"
path = "debounced.rs"

[[example]]
name = "poll_sysfs"
path = "poll_sysfs.rs"

[[example]]
name = "watcher_kind"
path = "watcher_kind.rs"

# specifically in its own sub folder
# to prevent audit from complaining
#[[example]]
#name = "hot_reload_tide"
30 changes: 30 additions & 0 deletions examples/debounced.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::{path::Path, time::Duration};

use notify::{RecursiveMode, Watcher};
use notify_debouncer_mini::new_debouncer;

fn main() {
std::thread::spawn(|| {
let path = Path::new("test.txt");
let _ = std::fs::remove_file(&path);
loop {
std::fs::write(&path, b"Lorem ipsum").unwrap();
std::thread::sleep(Duration::from_millis(250));
}
});

let (tx, rx) = std::sync::mpsc::channel();

let mut debouncer = new_debouncer(Duration::from_secs(2), None, tx).unwrap();

debouncer
.watcher()
.watch(Path::new("."), RecursiveMode::Recursive)
.unwrap();

for events in rx {
for e in events {
println!("{:?}", e);
}
}
}
31 changes: 31 additions & 0 deletions examples/debounced_full_custom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::{path::Path, time::Duration};

use notify::{RecursiveMode, Watcher};
use notify_debouncer_mini::new_debouncer;

/// Debouncer with custom backend and waiting for exit
fn main() {
std::thread::spawn(|| {
let path = Path::new("test.txt");
let _ = std::fs::remove_file(&path);
loop {
std::fs::write(&path, b"Lorem ipsum").unwrap();
std::thread::sleep(Duration::from_millis(250));
}
});

let (tx, rx) = std::sync::mpsc::channel();

let mut debouncer = new_debouncer_opt::<_,notify::PollWatcher>(Duration::from_secs(2), None, tx).unwrap();

debouncer
.watcher()
.watch(Path::new("."), RecursiveMode::Recursive)
.unwrap();

for events in rx {
for e in events {
println!("{:?}", e);
}
}
}
4 changes: 3 additions & 1 deletion examples/hot_reload_tide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ tide = "0.16.0"
async-std = { version = "1.6.0", features = ["attributes"] }
serde_json = "1.0"
serde = "1.0.115"
notify = { version = "5.0.0-pre.15", features = ["serde"] }
notify = { version = "5.0.0-pre.15", features = ["serde"], path = "../../notify" }

[workspace]
2 changes: 1 addition & 1 deletion examples/hot_reload_tide/tests/messages_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use hot_reload_tide::messages::{Config, load_config};
use hot_reload_tide::messages::{load_config, Config};

#[test]
fn load_config_from_file() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ fn main() {
} else {
Box::new(RecommendedWatcher::new(tx).unwrap())
};
// use _watcher here
}
10 changes: 0 additions & 10 deletions examples/watcher_kind/Cargo.toml

This file was deleted.

27 changes: 27 additions & 0 deletions notify-debouncer-mini/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "notify-debouncer-mini"
version = "0.1.0"
edition = "2021"
rust-version = "1.56"
description = "notify mini debouncer for events"
documentation = "https://docs.rs/notify_debouncer_mini"
homepage = "https://github.com/notify-rs/notify"
repository = "https://github.com/notify-rs/notify.git"
authors = ["Aron Heinecke <Ox0p54r36@t-online.de>"]
keywords = ["events", "filesystem", "notify", "watch"]
license = "CC0-1.0 OR Artistic-2.0"
readme = "README.md"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
name = "notify_debouncer_mini"
path = "src/lib.rs"

[features]
default = ["crossbeam-channel"]

[dependencies]
notify = "5.0.0-pre.15"
crossbeam-channel = { version = "0.5", optional = true }
serde = { version = "1.0.89", features = ["derive"], optional = true }
14 changes: 14 additions & 0 deletions notify-debouncer-mini/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Notify debouncer

[![» Docs](https://flat.badgen.net/badge/api/docs.rs/df3600)][docs]

Tiny debouncer for notify. Filters incoming events and emits only one event per timeframe per file.

## Features

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

[docs]: https://docs.rs/notify/0.1/notify-debouncer-mini/
Loading