Skip to content

Commit

Permalink
PollWatcher: unify signature of new and with_delay
Browse files Browse the repository at this point in the history
* Move `event_handler` logic from `new` to `with_delay` to be able to call:
```rust
let (tx, rx) = std::sync::mpsc::channel();
let mut watcher = PollWatcher::with_delay(tx, Duration::from_secs(1))?;
```
instead of:
```rust
let (tx, rx) = std::sync::mpsc::channel();
let mut watcher = PollWatcher::with_delay(Arc::new(Mutex::new(tx)), Duration::from_secs(1))?;
```

* Add documentation
  • Loading branch information
jhscheer authored and 0xpr03 committed Sep 29, 2021
1 parent 9d21a7a commit ff1a64c
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/poll.rs
Expand Up @@ -43,13 +43,10 @@ fn emit_event(event_handler: &Mutex<dyn EventHandler>, res: Result<Event>) {
}

impl PollWatcher {
/// Create a [PollWatcher] which polls every `delay` milliseconds
pub fn with_delay(
event_handler: Arc<Mutex<dyn EventHandler>>,
delay: Duration,
) -> Result<PollWatcher> {
/// Create a new [PollWatcher] and set the poll frequency to `delay`.
pub fn with_delay<F: EventHandler>(event_handler: F, delay: Duration) -> Result<PollWatcher> {
let mut p = PollWatcher {
event_handler,
event_handler: Arc::new(Mutex::new(event_handler)),
watches: Arc::new(Mutex::new(HashMap::new())),
open: Arc::new(AtomicBool::new(true)),
delay,
Expand Down Expand Up @@ -276,8 +273,10 @@ impl PollWatcher {

impl Watcher for PollWatcher {
/// Create a new [PollWatcher].
///
/// The default poll frequency is 30 seconds.
/// Use [with_delay] to manually set the poll frequency.
fn new<F: EventHandler>(event_handler: F) -> Result<Self> {
let event_handler = Arc::new(Mutex::new(event_handler));
let delay = Duration::from_secs(30);
Self::with_delay(event_handler, delay)
}
Expand Down

0 comments on commit ff1a64c

Please sign in to comment.