-
-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
182 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use anyhow::Result; | ||
use crossbeam_channel::{unbounded, Sender}; | ||
use notify::{Error, RecommendedWatcher, RecursiveMode}; | ||
use notify_debouncer_mini::{ | ||
new_debouncer, DebouncedEvent, Debouncer, | ||
}; | ||
use std::{ | ||
path::Path, sync::mpsc::RecvError, thread, time::Duration, | ||
}; | ||
|
||
pub struct RepoWatcher { | ||
receiver: crossbeam_channel::Receiver<()>, | ||
#[allow(dead_code)] | ||
debouncer: Debouncer<RecommendedWatcher>, | ||
} | ||
|
||
impl RepoWatcher { | ||
pub fn new(workdir: &str) -> Result<Self> { | ||
let (tx, rx) = std::sync::mpsc::channel(); | ||
|
||
let mut debouncer = | ||
new_debouncer(Duration::from_secs(2), None, tx)?; | ||
|
||
debouncer | ||
.watcher() | ||
.watch(Path::new(workdir), RecursiveMode::Recursive)?; | ||
|
||
let (out_tx, out_rx) = unbounded(); | ||
|
||
thread::spawn(move || { | ||
if let Err(e) = Self::forwarder(&rx, &out_tx) { | ||
//maybe we need to restart the forwarder now? | ||
log::error!("notify receive error: {}", e); | ||
} | ||
}); | ||
|
||
Ok(Self { | ||
debouncer, | ||
receiver: out_rx, | ||
}) | ||
} | ||
|
||
/// | ||
pub fn receiver(&self) -> crossbeam_channel::Receiver<()> { | ||
self.receiver.clone() | ||
} | ||
|
||
fn forwarder( | ||
receiver: &std::sync::mpsc::Receiver< | ||
Result<Vec<DebouncedEvent>, Vec<Error>>, | ||
>, | ||
sender: &Sender<()>, | ||
) -> Result<(), RecvError> { | ||
loop { | ||
let ev = receiver.recv()?; | ||
|
||
if let Ok(ev) = ev { | ||
log::debug!("notify events: {}", ev.len()); | ||
|
||
for (idx, ev) in ev.iter().enumerate() { | ||
log::debug!("notify [{}]: {:?}", idx, ev); | ||
} | ||
|
||
if !ev.is_empty() { | ||
sender.send(()).expect("send error"); | ||
} | ||
} | ||
} | ||
} | ||
} |