1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
use super::{
execute_sync_dispatcher_requests, ExecuteRequestsResult, FnsAndTraits, Listener, Mutex,
SyncDispatcherRequest,
};
use std::{
collections::{BTreeMap, HashMap},
hash::Hash,
rc::{Rc, Weak},
};
type PriorityListenerMap<P, T> = HashMap<T, BTreeMap<P, FnsAndTraits<T>>>;
/// In charge of prioritised sync dispatching to all listeners.
/// Owns a map event-variants and [`Weak`]-references to their
/// listeners and/or owns [`Fn`]s.
/// Opposed to [`EventListener`], this structure utilises one [`BTreeMap`] per
/// event-type to order listeners by a given priority-level.
///
/// **Note**: Consider implementing your own [`Ord`]-trait, if you
/// want a different kind of order.
///
/// [`Weak`]: https://doc.rust-lang.org/std/sync/struct.Weak.html
/// [`BTreeMap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html
/// [`Ord`]: https://doc.rust-lang.org/std/cmp/trait.Ord.html
/// [`EventListener`]: struct.EventDispatcher.html
/// [`Fn`]: https://doc.rust-lang.org/std/ops/trait.Fn.html
pub struct PriorityEventDispatcher<P, T>
where
P: Ord,
T: PartialEq + Eq + Hash + Clone + 'static,
{
events: PriorityListenerMap<P, T>,
}
impl<P, T> Default for PriorityEventDispatcher<P, T>
where
P: Ord + Clone,
T: PartialEq + Eq + Hash + Clone + 'static,
{
fn default() -> PriorityEventDispatcher<P, T> {
PriorityEventDispatcher {
events: PriorityListenerMap::new(),
}
}
}
impl<P, T> PriorityEventDispatcher<P, T>
where
P: Ord + Clone,
T: PartialEq + Eq + Hash + Clone + 'static,
{
/// Adds a [`Listener`] to listen for an `event_identifier`, considering
/// a given `priority` implementing the [`Ord`]-trait, to sort dispatch-order.
/// If `event_identifier` is a new [`HashMap`]-key, it will be added.
///
/// **Note**: If your `Enum` owns fields you need to consider implementing
/// the [`Hash`]- and [`PartialEq`]-trait if you want to ignore fields,
/// see second example for an implementation-suggestion.
///
/// # Examples
///
/// Adding a [`Listener`] to the dispatcher:
///
/// ```rust
/// extern crate hey_listen;
///
/// use std::rc::Rc;
/// use hey_listen::{Listener, Mutex, rc::priority_dispatcher::PriorityEventDispatcher, SyncDispatcherRequest};
///
/// #[derive(Clone, Eq, Hash, PartialEq)]
/// enum Event {
/// EventType,
/// }
///
/// struct ListenerStruct {}
///
/// impl Listener<Event> for ListenerStruct {
/// fn on_event(&mut self, event: &Event) -> Option<SyncDispatcherRequest> { None }
/// }
///
/// fn main() {
/// let listener = Rc::new(Mutex::new(ListenerStruct {}));
/// let mut dispatcher: PriorityEventDispatcher<u32, Event> = PriorityEventDispatcher::default();
///
/// dispatcher.add_listener(Event::EventType, &listener, 1);
/// }
/// ```
///
/// Declaring your own [`Hash`]- and [`PartialEq`]-trait to bypass
/// hashing on fields:
///
/// ```rust
/// use std::hash::{Hash, Hasher};
/// use std::mem::discriminant;
///
/// #[derive(Clone)]
/// enum Event {
/// TestVariant(i32),
/// }
///
/// impl Hash for Event {
/// fn hash<H: Hasher>(&self, _state: &mut H) {}
/// }
///
/// impl PartialEq for Event {
/// fn eq(&self, other: &Event) -> bool {
/// discriminant(self) == discriminant(other)
/// }
/// }
///
/// impl Eq for Event {}
/// ```
///
/// [`EventDispatcher`]: struct.EventDispatcher.html
/// [`Listener`]: trait.Listener.html
/// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html
/// [`PartialEq`]: https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
/// [`HashMap`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html
/// [`Ord`]: https://doc.rust-lang.org/std/cmp/trait.Ord.html
pub fn add_listener<D: Listener<T> + 'static>(
&mut self,
event_identifier: T,
listener: &Rc<Mutex<D>>,
priority: P,
) {
if let Some(prioritised_listener_collection) = self.events.get_mut(&event_identifier) {
if let Some(priority_level_collection) =
prioritised_listener_collection.get_mut(&priority)
{
priority_level_collection.traits.push(Rc::downgrade(
&(Rc::clone(listener) as Rc<Mutex<Listener<T> + 'static>>),
));
return;
}
prioritised_listener_collection.insert(
priority.clone(),
FnsAndTraits::new_with_traits(vec![Rc::downgrade(
&(Rc::clone(listener) as Rc<Mutex<Listener<T> + 'static>>),
)]),
);
return;
}
let mut b_tree_map = BTreeMap::new();
b_tree_map.insert(
priority,
FnsAndTraits::new_with_traits(vec![Rc::downgrade(
&(Rc::clone(listener) as Rc<Mutex<Listener<T> + 'static>>),
)]),
);
self.events.insert(event_identifier, b_tree_map);
}
/// Adds an [`Fn`] to listen for an `event_identifier`, considering
/// a given `priority` implementing the [`Ord`]-trait in order to sort dispatch-order.
/// If `event_identifier` is a new [`HashMap`]-key, it will be added.
///
/// # Examples
///
/// Adding an [`Fn`] to the dispatcher:
///
/// ```rust
/// extern crate hey_listen;
///
/// use hey_listen::{Mutex, rc::priority_dispatcher::PriorityEventDispatcher, SyncDispatcherRequest};
/// use std::rc::Rc;
///
/// #[derive(Clone, Eq, Hash, PartialEq)]
/// enum Event {
/// EventType,
/// }
///
/// struct EventListener {
/// used_method: bool,
/// }
///
/// impl EventListener {
/// fn test_method(&mut self, _event: &Event) {
/// self.used_method = true;
/// }
/// }
///
/// fn main() {
/// let listener = Rc::new(Mutex::new(EventListener { used_method: false }));
/// let mut dispatcher: PriorityEventDispatcher<u32, Event> = PriorityEventDispatcher::default();
/// let weak_listener_ref = Rc::downgrade(&Rc::clone(&listener));
///
/// let closure = Box::new(move |event: &Event| -> Option<SyncDispatcherRequest> {
/// if let Some(listener) = weak_listener_ref.upgrade() {
/// listener.lock().test_method(&event);
///
/// None
/// } else {
/// Some(SyncDispatcherRequest::StopListening)
/// }
/// });
///
/// dispatcher.add_fn(Event::EventType, closure, 1);
/// }
/// ```
///
/// [`Fn`]: https://doc.rust-lang.org/std/ops/trait.Fn.html
/// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html
/// [`PartialEq`]: https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
/// [`HashMap`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html
pub fn add_fn(
&mut self,
event_identifier: T,
function: Box<Fn(&T) -> Option<SyncDispatcherRequest>>,
priority: P,
) {
if let Some(prioritised_listener_collection) = self.events.get_mut(&event_identifier) {
if let Some(priority_level_collection) =
prioritised_listener_collection.get_mut(&priority)
{
priority_level_collection.fns.push(function);
return;
}
prioritised_listener_collection
.insert(priority.clone(), FnsAndTraits::new_with_fns(vec![function]));
return;
}
let mut b_tree_map = BTreeMap::new();
b_tree_map.insert(priority, FnsAndTraits::new_with_fns(vec![function]));
self.events.insert(event_identifier, b_tree_map);
}
/// All [`Listener`]s listening to a passed `event_identifier`
/// will be called via their implemented [`on_event`]-method.
/// [`Fn`]s returning [`Result`] with `Ok(())` will be retained
/// and `Err(SyncDispatcherRequest::StopListening)` will cause them to
/// be removed from the event-dispatcher.
///
/// **Notice**: [`Listener`]s will called ordered by their priority-level.
///
/// [`Listener`]: trait.Listener.html
/// [`on_event`]: trait.Listener.html#tymethod.on_event
/// [`Fn`]: https://doc.rust-lang.org/std/ops/trait.Fn.html
/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
pub fn dispatch_event(&mut self, event_identifier: &T) {
if let Some(prioritised_listener_collection) = self.events.get_mut(event_identifier) {
for (_, listener_collection) in prioritised_listener_collection.iter_mut() {
let mut found_invalid_weak_ref = false;
if let ExecuteRequestsResult::Stopped = execute_sync_dispatcher_requests(
&mut listener_collection.traits,
|weak_listener| {
if let Some(listener_arc) = weak_listener.upgrade() {
let mut listener = listener_arc.lock();
listener.on_event(event_identifier)
} else {
found_invalid_weak_ref = true;
None
}
},
) {
break;
}
if let ExecuteRequestsResult::Stopped =
execute_sync_dispatcher_requests(&mut listener_collection.fns, |callback| {
callback(event_identifier)
}) {
break;
}
if found_invalid_weak_ref {
listener_collection
.traits
.retain(|listener| Weak::clone(listener).upgrade().is_some());
}
}
}
}
}