1use std::{
2 path::Path,
3 pin::Pin,
4 task::{Context, Poll},
5};
6
7pub use notify;
8
9use futures_channel::mpsc;
10use futures_core::stream::Stream;
11use notify::{RecommendedWatcher, RecursiveMode, Watcher};
12
13type StreamItem = notify::Result<notify::Event>;
14pub struct NotifyStream(mpsc::UnboundedReceiver<StreamItem>, RecommendedWatcher);
15
16impl Stream for NotifyStream {
17 type Item = StreamItem;
18
19 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
20 Pin::new(&mut self.0).poll_next(cx)
21 }
22}
23
24pub fn notify_stream<P: AsRef<Path>>(
25 path: P,
26 recursive_mode: RecursiveMode,
27) -> notify::Result<NotifyStream> {
28 let (tx, rx) = mpsc::unbounded();
29
30 let mut watcher: RecommendedWatcher =
31 Watcher::new_immediate(move |i: notify::Result<notify::Event>| {
32 let _ = tx.unbounded_send(i).ok();
34 })?;
35
36 watcher.watch(path, recursive_mode)?;
37
38 Ok(NotifyStream(rx, watcher))
39}