-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.rs
More file actions
145 lines (127 loc) · 3.1 KB
/
Copy pathworker.rs
File metadata and controls
145 lines (127 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* src/worker.rs */
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use tokio::sync::broadcast;
use tokio::sync::mpsc;
use crate::target::CompiledTarget;
use crate::{Config, Event, EventKind};
struct DebounceState {
last_seen: Instant,
kind: EventKind,
}
pub(crate) async fn process_events(
mut raw_rx: mpsc::Receiver<notify::Result<notify::Event>>,
user_tx: broadcast::Sender<Event>,
target: CompiledTarget,
config: Config,
root_path: PathBuf,
) {
let mut pending: HashMap<PathBuf, DebounceState> = HashMap::new();
let tick_rate = if config.debounce < Duration::from_millis(50) {
config.debounce
} else {
config.debounce / 5
};
let mut interval = tokio::time::interval(tick_rate);
loop {
tokio::select! {
maybe_event = raw_rx.recv() => {
match maybe_event {
Some(Ok(event)) => {
handle_raw_event(event, &mut pending, &target, &config, &root_path);
}
Some(Err(e)) => {
#[cfg(feature = "logging")]
tracing::error!("Notify error: {:?}", e);
#[cfg(not(feature = "logging"))]
let _ = e;
}
None => break,
}
}
_ = interval.tick() => {
flush_pending(&mut pending, &user_tx, &config).await;
}
}
}
}
fn handle_raw_event(
event: notify::Event,
pending: &mut HashMap<PathBuf, DebounceState>,
target: &CompiledTarget,
config: &Config,
root_path: &Path,
) {
use notify::EventKind as NK;
let kind = match event.kind {
NK::Create(_) => EventKind::Create,
NK::Modify(_) => EventKind::Modify,
NK::Remove(_) => EventKind::Remove,
_ => return,
};
for path in event.paths {
if !target.matches(&path, config, root_path) {
continue;
}
pending
.entry(path.clone())
.and_modify(|state| {
let now = Instant::now();
state.last_seen = now;
let prev_kind = state.kind;
if !config.coalesce {
state.kind = kind;
return;
}
match (prev_kind, kind) {
(EventKind::Create, EventKind::Modify) => { /* Keep Create */ }
(EventKind::Create, EventKind::Remove) => {
state.kind = EventKind::Remove;
}
(EventKind::Modify, EventKind::Remove) => {
state.kind = EventKind::Remove;
}
(EventKind::Remove, EventKind::Modify) => {
// Ignore noise
}
_ => {
state.kind = kind;
}
}
})
.or_insert(DebounceState {
last_seen: Instant::now(),
kind,
});
}
}
async fn flush_pending(
pending: &mut HashMap<PathBuf, DebounceState>,
tx: &broadcast::Sender<Event>,
config: &Config,
) {
let now = Instant::now();
let mut to_remove = Vec::new();
for (path, state) in pending.iter() {
if now.duration_since(state.last_seen) >= config.debounce {
let kind = state.kind;
let allowed = match &config.listen_events {
None => true,
Some(list) => list.contains(&kind),
};
if allowed {
let event = Event {
paths: vec![path.clone()],
kind,
};
// Broadcast error usually means no subscribers, which is fine.
let _ = tx.send(event);
}
to_remove.push(path.clone());
}
}
for path in to_remove {
pending.remove(&path);
}
}