Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 68 additions & 5 deletions packages/app-lib/src/state/instances/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ use crate::state::{
};
use crate::worlds::WorldType;
use dashmap::{DashMap, mapref::entry::Entry};
use notify::{RecommendedWatcher, RecursiveMode};
use notify_debouncer_mini::{DebounceEventResult, Debouncer, new_debouncer};
use notify::event::{AccessKind, AccessMode};
use notify::{
Config, Event, EventHandler, EventKind, RecommendedWatcher, RecursiveMode,
Watcher, WatcherKind,
};
use notify_debouncer_mini::{
DebounceEventResult, Debouncer, new_debouncer_opt,
};
use std::path::Path;
use std::sync::LazyLock;
use std::{
collections::{HashMap, HashSet},
Expand All @@ -20,10 +27,65 @@ use tokio::sync::{RwLock, mpsc::channel};
use super::adapters::sqlite::instance_rows;

pub struct FileWatcher {
watcher: RwLock<Debouncer<RecommendedWatcher>>,
watcher: RwLock<Debouncer<WriteOnlyWatcher>>,
instance_ids: Arc<RwLock<HashMap<String, String>>>,
}

/// Wraps the platform watcher and drops read-only access events before they
/// reach the debouncer.
///
/// The inotify backend reports `IN_OPEN`/`IN_CLOSE_NOWRITE`, and
/// `notify-debouncer-mini` discards event kinds, so without this every read
/// of a watched file (e.g. the locale indexer reading `options.txt`) looks
/// like a change and can re-trigger the reader in a loop.
pub struct WriteOnlyWatcher(RecommendedWatcher);

fn is_read_only_access(kind: &EventKind) -> bool {
matches!(kind, EventKind::Access(access)
if *access != AccessKind::Close(AccessMode::Write))
}

impl Watcher for WriteOnlyWatcher {
fn new<F: EventHandler>(
mut event_handler: F,
config: Config,
) -> notify::Result<Self> {
RecommendedWatcher::new(
move |event: notify::Result<Event>| {
if event
.as_ref()
.is_ok_and(|event| is_read_only_access(&event.kind))
{
return;
}
event_handler.handle_event(event);
},
config,
)
.map(Self)
}

fn watch(
&mut self,
path: &Path,
recursive_mode: RecursiveMode,
) -> notify::Result<()> {
self.0.watch(path, recursive_mode)
}

fn unwatch(&mut self, path: &Path) -> notify::Result<()> {
self.0.unwatch(path)
}

fn configure(&mut self, option: Config) -> notify::Result<bool> {
self.0.configure(option)
}

fn kind() -> WatcherKind {
RecommendedWatcher::kind()
}
}

static CONTENT_SYNCS: LazyLock<DashMap<String, bool>> =
LazyLock::new(DashMap::new);

Expand Down Expand Up @@ -97,8 +159,9 @@ pub async fn init_watcher() -> crate::Result<FileWatcher> {
let instance_ids = Arc::new(RwLock::new(HashMap::<String, String>::new()));
let event_instance_ids = instance_ids.clone();

let file_watcher = new_debouncer(
Duration::from_secs_f32(1.0),
let file_watcher = new_debouncer_opt::<_, WriteOnlyWatcher>(
notify_debouncer_mini::Config::default()
.with_timeout(Duration::from_secs_f32(1.0)),
move |res: DebounceEventResult| {
tx.blocking_send(res).ok();
},
Expand Down
Loading