From 40fa0b491f06500347f1ae94f088734cdad4f498 Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Mon, 23 Dec 2024 14:46:06 +0100 Subject: [PATCH] extract event handler --- kitsune/src/template.rs | 50 ++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/kitsune/src/template.rs b/kitsune/src/template.rs index a36b7cfd5..f851e2768 100644 --- a/kitsune/src/template.rs +++ b/kitsune/src/template.rs @@ -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"));