cosmic_files/operation/
notifiers.rs1use std::path::{Path, PathBuf};
5use std::sync::{Arc, LazyLock, Mutex};
6use tokio::sync::Notify;
7
8pub struct FileWritingNotifier {
10 data: Vec<PathBuf>,
11 notify: Arc<Notify>,
12}
13
14static ACTIVELY_WRITING: LazyLock<Mutex<FileWritingNotifier>> = LazyLock::new(|| {
15 Mutex::new(FileWritingNotifier {
16 data: Vec::new(),
17 notify: Arc::new(Notify::new()),
18 })
19});
20
21pub fn actively_writing_add(path: PathBuf) {
23 ACTIVELY_WRITING.lock().unwrap().data.push(path);
24}
25
26pub fn actively_writing_remove(path: &Path) {
28 let mut guard = ACTIVELY_WRITING.lock().unwrap();
29 guard.data.retain(|p| p != path);
30 guard.notify.notify_waiters();
31}
32
33pub async fn actively_writing_tick() {
35 let notify = (|| {
36 let guard = ACTIVELY_WRITING.lock().unwrap();
37
38 if !guard.data.is_empty() {
39 return Some(guard.notify.clone());
40 }
41
42 None
43 })();
44
45 if let Some(notify) = notify {
46 notify.notified().await
47 }
48}
49
50pub fn is_actively_writing_to(path: &Path) -> bool {
52 ACTIVELY_WRITING
53 .lock()
54 .unwrap()
55 .data
56 .iter()
57 .any(|p| p == path)
58}