Skip to content

Commit

Permalink
debounce notify events
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Nov 30, 2024
1 parent 7aa9060 commit a645fdc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
24 changes: 23 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kitsune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
45 changes: 25 additions & 20 deletions kitsune/src/template.rs
Original file line number Diff line number Diff line change
@@ -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<ArcSwapAny<Arc<minijinja::Environment<'static>>>> = OnceLock::new();
Expand Down Expand Up @@ -30,28 +30,33 @@ fn init_environment() -> minijinja::Environment<'static> {
}

fn spawn_watcher() {
let watcher = notify::recommended_watcher(|event: notify::Result<notify::Event>| {
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;

Check warning on line 38 in kitsune/src/template.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/template.rs#L33-L38

Added lines #L33 - L38 were not covered by tests
};

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,

Check warning on line 43 in kitsune/src/template.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/template.rs#L41-L43

Added lines #L41 - L43 were not covered by tests
notify::Event {
kind: notify::EventKind::Create(..)
| notify::EventKind::Modify(..)
| notify::EventKind::Remove(..),
..
}
) {
debug!("reloading templates");

Check warning on line 51 in kitsune/src/template.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/template.rs#L51

Added line #L51 was not covered by tests

if let Some(env) = ENVIRONMENT.get() {
env.store(Arc::new(init_environment()));
if let Some(env) = ENVIRONMENT.get() {
env.store(Arc::new(init_environment()));
}

Check warning on line 55 in kitsune/src/template.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/template.rs#L53-L55

Added lines #L53 - L55 were not covered by tests
}
}
_ => return,
}
})
},
)

Check warning on line 59 in kitsune/src/template.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/template.rs#L58-L59

Added lines #L58 - L59 were not covered by tests
.unwrap();

let mut watcher = ManuallyDrop::new(watcher);
Expand Down

0 comments on commit a645fdc

Please sign in to comment.