1
1
use anyhow:: Result ;
2
2
use crossbeam_channel:: { unbounded, Sender } ;
3
- use notify:: { Error , RecommendedWatcher , RecursiveMode } ;
3
+ use notify:: {
4
+ Config , Error , PollWatcher , RecommendedWatcher , RecursiveMode ,
5
+ Watcher ,
6
+ } ;
4
7
use notify_debouncer_mini:: {
5
- new_debouncer, DebouncedEvent , Debouncer ,
8
+ new_debouncer, new_debouncer_opt , DebouncedEvent ,
6
9
} ;
7
10
use scopetime:: scope_time;
8
11
use std:: {
@@ -11,22 +14,23 @@ use std::{
11
14
12
15
pub struct RepoWatcher {
13
16
receiver : crossbeam_channel:: Receiver < ( ) > ,
14
- #[ allow( dead_code) ]
15
- debouncer : Debouncer < RecommendedWatcher > ,
16
17
}
17
18
18
19
impl RepoWatcher {
19
- pub fn new ( workdir : & str ) -> Result < Self > {
20
- scope_time ! ( "RepoWatcher::new" ) ;
20
+ pub fn new ( workdir : & str , poll : bool ) -> Self {
21
+ log:: trace!(
22
+ "poll watcher: {poll} recommended: {:?}" ,
23
+ RecommendedWatcher :: kind( )
24
+ ) ;
21
25
22
26
let ( tx, rx) = std:: sync:: mpsc:: channel ( ) ;
23
27
24
- let mut debouncer =
25
- new_debouncer ( Duration :: from_secs ( 2 ) , None , tx) ?;
28
+ let workdir = workdir. to_string ( ) ;
26
29
27
- debouncer
28
- . watcher ( )
29
- . watch ( Path :: new ( workdir) , RecursiveMode :: Recursive ) ?;
30
+ thread:: spawn ( move || {
31
+ let timeout = Duration :: from_secs ( 2 ) ;
32
+ create_watcher ( poll, timeout, tx, & workdir) ;
33
+ } ) ;
30
34
31
35
let ( out_tx, out_rx) = unbounded ( ) ;
32
36
@@ -37,10 +41,7 @@ impl RepoWatcher {
37
41
}
38
42
} ) ;
39
43
40
- Ok ( Self {
41
- debouncer,
42
- receiver : out_rx,
43
- } )
44
+ Self { receiver : out_rx }
44
45
}
45
46
46
47
///
@@ -71,3 +72,38 @@ impl RepoWatcher {
71
72
}
72
73
}
73
74
}
75
+
76
+ fn create_watcher (
77
+ poll : bool ,
78
+ timeout : Duration ,
79
+ tx : std:: sync:: mpsc:: Sender <
80
+ Result < Vec < DebouncedEvent > , Vec < Error > > ,
81
+ > ,
82
+ workdir : & str ,
83
+ ) {
84
+ scope_time ! ( "create_watcher" ) ;
85
+
86
+ if poll {
87
+ let config = Config :: default ( )
88
+ . with_poll_interval ( Duration :: from_secs ( 2 ) ) ;
89
+ let mut bouncer = new_debouncer_opt :: < _ , PollWatcher > (
90
+ timeout, None , tx, config,
91
+ )
92
+ . expect ( "Watch create error" ) ;
93
+ bouncer
94
+ . watcher ( )
95
+ . watch ( Path :: new ( & workdir) , RecursiveMode :: Recursive )
96
+ . expect ( "Watch error" ) ;
97
+
98
+ std:: mem:: forget ( bouncer) ;
99
+ } else {
100
+ let mut bouncer = new_debouncer ( timeout, None , tx)
101
+ . expect ( "Watch create error" ) ;
102
+ bouncer
103
+ . watcher ( )
104
+ . watch ( Path :: new ( & workdir) , RecursiveMode :: Recursive )
105
+ . expect ( "Watch error" ) ;
106
+
107
+ std:: mem:: forget ( bouncer) ;
108
+ } ;
109
+ }
0 commit comments