From a645fdc4678707b298a10ad0c99cf55f1832e32c Mon Sep 17 00:00:00 2001 From: aumetra Date: Sat, 30 Nov 2024 20:39:47 +0100 Subject: [PATCH] debounce notify events --- Cargo.lock | 24 +++++++++++++++++++++- kitsune/Cargo.toml | 2 +- kitsune/src/template.rs | 45 +++++++++++++++++++++++------------------ 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6762099e..7998751c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2124,6 +2124,15 @@ version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" +[[package]] +name = "file-id" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bc904b9bbefcadbd8e3a9fb0d464a9b979de6324c03b3c663e8994f46a5be36" +dependencies = [ + "windows-sys 0.52.0", +] + [[package]] name = "filetime" version = "0.2.25" @@ -3378,7 +3387,7 @@ dependencies = [ "mime", "mime_guess", "minijinja", - "notify", + "notify-debouncer-full", "oxide-auth", "oxide-auth-async", "oxide-auth-axum", @@ -4628,6 +4637,19 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "notify-debouncer-full" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dcf855483228259b2353f89e99df35fc639b2b2510d1166e4858e3f67ec1afb" +dependencies = [ + "file-id", + "log", + "notify", + "notify-types", + "walkdir", +] + [[package]] name = "notify-types" version = "1.0.0" diff --git a/kitsune/Cargo.toml b/kitsune/Cargo.toml index ee404de4..800fdc2a 100644 --- a/kitsune/Cargo.toml +++ b/kitsune/Cargo.toml @@ -71,7 +71,7 @@ mimalloc = "0.1.43" mime = "0.3.17" mime_guess = { version = "2.0.5", default-features = false } minijinja.workspace = true -notify = "7.0.0" +notify-debouncer-full = "0.4.0" oxide-auth = "0.6.1" oxide-auth-async = "0.2.1" oxide-auth-axum = "0.5.0" diff --git a/kitsune/src/template.rs b/kitsune/src/template.rs index 81d357f3..94a9666c 100644 --- a/kitsune/src/template.rs +++ b/kitsune/src/template.rs @@ -1,8 +1,8 @@ use arc_swap::ArcSwapAny; use core::str; -use notify::Watcher; +use notify_debouncer_full::notify; use rust_embed::RustEmbed; -use std::{mem::ManuallyDrop, path::Path, sync::OnceLock}; +use std::{mem::ManuallyDrop, path::Path, sync::OnceLock, time::Duration}; use triomphe::Arc; static ENVIRONMENT: OnceLock>>> = OnceLock::new(); @@ -30,28 +30,33 @@ fn init_environment() -> minijinja::Environment<'static> { } fn spawn_watcher() { - let watcher = notify::recommended_watcher(|event: notify::Result| { - if event.is_err() { - return; - } + let watcher = notify_debouncer_full::new_debouncer( + Duration::from_secs(1), + None, + |events: notify_debouncer_full::DebounceEventResult| { + let Ok(events) = events else { + return; + }; - match event { - Ok(notify::Event { - kind: - notify::EventKind::Create(..) - | notify::EventKind::Modify(..) - | notify::EventKind::Remove(..), - .. - }) => { - debug!("reloading templates"); + for event in events { + if matches!( + event.event, + notify::Event { + kind: notify::EventKind::Create(..) + | notify::EventKind::Modify(..) + | notify::EventKind::Remove(..), + .. + } + ) { + debug!("reloading templates"); - if let Some(env) = ENVIRONMENT.get() { - env.store(Arc::new(init_environment())); + if let Some(env) = ENVIRONMENT.get() { + env.store(Arc::new(init_environment())); + } } } - _ => return, - } - }) + }, + ) .unwrap(); let mut watcher = ManuallyDrop::new(watcher);