Skip to content

Commit

Permalink
extract event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Dec 23, 2024
1 parent 64bf4d9 commit 40fa0b4
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions kitsune/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,33 @@ fn init_environment() -> minijinja::Environment<'static> {
environment
}

fn spawn_watcher() {
let watcher = notify_debouncer_full::new_debouncer(
Duration::from_secs(1),
None,
|events: DebounceEventResult| {
let Ok(events) = events else {
return;
};
fn event_handler(events: DebounceEventResult) {
let Ok(events) = events else {
return;
};

for event in events {
if matches!(
event.event,
notify::Event {
kind: notify::EventKind::Create(..)
| notify::EventKind::Modify(..)
| notify::EventKind::Remove(..),
..
}
) {
debug!(?event.paths, "reloading templates");
for event in events {
if matches!(
event.event,
notify::Event {
kind: notify::EventKind::Create(..)
| notify::EventKind::Modify(..)
| notify::EventKind::Remove(..),
..
}
) {
debug!(?event.paths, "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()));
}
},
)
.unwrap();
}
}
}

fn spawn_watcher() {
let watcher =
notify_debouncer_full::new_debouncer(Duration::from_secs(1), None, event_handler).unwrap();

let mut watcher = ManuallyDrop::new(watcher);
let template_dir = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates"));
Expand Down

0 comments on commit 40fa0b4

Please sign in to comment.