This commit is contained in:
sxyazi 2024-04-05 17:15:08 +08:00
parent 0a04a3cdd4
commit d1adf723e5
No known key found for this signature in database
2 changed files with 34 additions and 41 deletions

View file

@ -14,14 +14,18 @@ jobs:
permissions: permissions:
issues: write issues: write
pull-requests: write pull-requests: write
discussions: write
steps: steps:
- uses: dessant/lock-threads@v4 - uses: dessant/lock-threads@v4
with: with:
issue-inactive-days: "30" issue-inactive-days: "30"
issue-comment: > issue-comment: >
I'm going to lock this issue because it has been closed for _30 days_. ⏳ I'm going to lock this issue because it has been closed for _30 days_. ⏳
This helps our maintainers find and focus on the active issues. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new If you have found a problem that seems similar to this, please open a new
issue and complete the issue template so we can capture all the details issue and complete the issue template so we can capture all the details
necessary to investigate further. necessary to investigate further.
process-only: "issues" pr-inactive-days: "30"
discussion-inactive-days: "30"
process-only: "issues,prs,discussions"

View file

@ -17,7 +17,7 @@ pub(crate) static WATCHED: RoCell<RwLock<HashSet<Url>>> = RoCell::new();
pub static LINKED: RoCell<RwLock<Linked>> = RoCell::new(); pub static LINKED: RoCell<RwLock<Linked>> = RoCell::new();
pub struct Watcher { pub struct Watcher {
tx: watch::Sender<(HashSet<Url>, HashSet<Url>)>, tx: watch::Sender<HashSet<Url>>,
} }
impl Watcher { impl Watcher {
@ -60,16 +60,7 @@ impl Watcher {
pub(super) fn watch(&mut self, mut new: HashSet<&Url>) { pub(super) fn watch(&mut self, mut new: HashSet<&Url>) {
new.retain(|&u| u.is_regular()); new.retain(|&u| u.is_regular());
self.tx.send(new.into_iter().cloned().collect()).ok();
let old = WATCHED.read();
let old: HashSet<_> = old.iter().collect();
let (to_unwatch, to_watch): (HashSet<_>, HashSet<_>) = (
old.difference(&new).map(|&x| x.clone()).collect(),
new.difference(&old).map(|&x| x.clone()).collect(),
);
self.tx.send((to_unwatch, to_watch)).ok();
} }
pub(super) fn trigger_dirs(&self, folders: &[&Folder]) { pub(super) fn trigger_dirs(&self, folders: &[&Folder]) {
@ -103,23 +94,20 @@ impl Watcher {
}); });
} }
async fn on_in( async fn on_in(mut rx: watch::Receiver<HashSet<Url>>, mut watcher: RecommendedWatcher) {
mut rx: watch::Receiver<(HashSet<Url>, HashSet<Url>)>,
mut watcher: RecommendedWatcher,
) {
loop { loop {
let (mut to_unwatch, mut to_watch): (HashSet<_>, HashSet<_>) = {
let (new, old) = (&*rx.borrow_and_update(), &*WATCHED.read());
(old.difference(new).cloned().collect(), new.difference(old).cloned().collect())
};
to_unwatch.retain(|u| watcher.unwatch(u).is_ok());
to_watch.retain(|u| watcher.watch(u, RecursiveMode::NonRecursive).is_ok());
{ {
let (ref to_unwatch, ref to_watch) = *rx.borrow_and_update(); let mut watched = WATCHED.write();
for u in to_unwatch { watched.retain(|u| !to_unwatch.contains(u));
if watcher.unwatch(u).is_ok() { watched.extend(to_watch);
WATCHED.write().remove(u);
}
}
for u in to_watch {
if watcher.watch(u, RecursiveMode::NonRecursive).is_ok() {
WATCHED.write().insert(u.clone());
}
}
} }
if !rx.has_changed().unwrap_or(false) { if !rx.has_changed().unwrap_or(false) {
@ -166,23 +154,24 @@ impl Watcher {
async fn sync_linked() { async fn sync_linked() {
let mut new = WATCHED.read().clone(); let mut new = WATCHED.read().clone();
LINKED.write().retain(|k, _| new.remove(k));
macro_rules! go { let old = {
($todo:expr) => { let mut linked = LINKED.write();
for from in $todo { linked.retain(|k, _| new.remove(k));
match fs::canonicalize(&from).await { linked.keys().cloned().collect()
Ok(to) if to != *from && WATCHED.read().contains(&from) => { };
async fn go(todo: HashSet<Url>) {
for from in todo {
let Ok(to) = fs::canonicalize(&from).await else { continue };
if to != *from && WATCHED.read().contains(&from) {
LINKED.write().insert(from, Url::from(to)); LINKED.write().insert(from, Url::from(to));
} }
_ => {}
} }
} }
};
}
let old: Vec<_> = LINKED.read().keys().cloned().collect(); go(new).await;
go!(new); go(old).await;
go!(old);
} }
} }