From d8057575ce35ee4b937b7bb58437e0171dfd959b Mon Sep 17 00:00:00 2001 From: Stu Black Date: Mon, 8 Dec 2025 13:14:12 -0500 Subject: [PATCH] Use a helper method for adding notification listeners. --- wutil-rs/src/notification.rs | 46 ++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/wutil-rs/src/notification.rs b/wutil-rs/src/notification.rs index 1903ce03..d43d800d 100644 --- a/wutil-rs/src/notification.rs +++ b/wutil-rs/src/notification.rs @@ -1,11 +1,28 @@ use std::{ collections::{hash_map::Entry, HashMap}, ffi::{c_void, CStr}, - hash, + hash::{self, Hash}, ptr::{self, NonNull}, sync::{Mutex, OnceLock}, }; +// Helper function for adding the entry `(key, (observer, action))` to `map`. +fn register( + map: &mut HashMap, Action)>>, + key: K, + observer: Option, + action: Action, +) { + match map.entry(key) { + Entry::Occupied(mut o) => { + o.get_mut().push((observer, action)); + } + Entry::Vacant(v) => { + v.insert(vec![(observer, action)]); + } + } +} + /// Lightweight message from object to another. When a notification is sent, /// registered [`Action`]s are called. /// @@ -119,14 +136,7 @@ impl NotificationCenter { observer: Option, action: Action, ) { - match self.exact.entry((name, source)) { - Entry::Occupied(mut o) => { - o.get_mut().push((observer, action)); - } - Entry::Vacant(v) => { - v.insert(vec![(observer, action)]); - } - } + register(&mut self.exact, (name, source), observer, action); } /// Registers `action` to be invoked on `observer` when notifications are @@ -137,14 +147,7 @@ impl NotificationCenter { observer: Option, action: Action, ) { - match self.by_source.entry(source) { - Entry::Occupied(mut o) => { - o.get_mut().push((observer, action)); - } - Entry::Vacant(v) => { - v.insert(vec![(observer, action)]); - } - } + register(&mut self.by_source, source, observer, action); } /// Registers `action` to be invoked on `observer` for all notifications @@ -155,14 +158,7 @@ impl NotificationCenter { observer: Option, action: Action, ) { - match self.by_name.entry(name) { - Entry::Occupied(mut o) => { - o.get_mut().push((observer, action)); - } - Entry::Vacant(v) => { - v.insert(vec![(observer, action)]); - } - } + register(&mut self.by_name, name, observer, action); } /// Registers `action` to be invoked on `observer` for all notifications,