cooklang_sync_client/
file_watcher.rs

1use futures::{
2    channel::mpsc::{channel, Receiver},
3    SinkExt,
4};
5use notify::RecommendedWatcher;
6use notify_debouncer_mini::{new_debouncer, DebounceEventResult, Debouncer};
7use std::time::Duration;
8
9const CHANNEL_SIZE: usize = 1000;
10const DEBOUNCE_SEC: u64 = 2;
11
12pub fn async_watcher(
13) -> notify::Result<(Debouncer<RecommendedWatcher>, Receiver<DebounceEventResult>)> {
14    let (mut tx, rx) = channel(CHANNEL_SIZE);
15
16    let debouncer = new_debouncer(
17        Duration::from_secs(DEBOUNCE_SEC),
18        move |res: DebounceEventResult| {
19            futures::executor::block_on(async {
20                let _ = tx.send(res).await;
21            })
22        },
23    )?;
24
25    Ok((debouncer, rx))
26}