|
| 1 | +use std::cell::{Ref, RefCell, RefMut}; |
| 2 | +use std::rc::{Rc, Weak}; |
| 3 | + |
| 4 | +/// Events that can be emitted represented using an enum |
| 5 | +pub enum EditorEvent { |
| 6 | + Mention { person: String }, |
| 7 | + Comment { person: String, comment: String }, |
| 8 | +} |
| 9 | + |
| 10 | +/// Interface for a listener |
| 11 | +pub trait ListenerUpdate { |
| 12 | + fn update(&mut self, event: &EditorEvent); |
| 13 | +} |
| 14 | + |
| 15 | +/// `Listener` wraps the updater `EventListener` and gives out references. |
| 16 | +pub struct ListenerCell<T> { |
| 17 | + rc: Rc<RefCell<T>>, |
| 18 | +} |
| 19 | + |
| 20 | +impl<T> ListenerCell<T> |
| 21 | +where |
| 22 | + T: ListenerUpdate + 'static, |
| 23 | +{ |
| 24 | + pub fn from(listener: T) -> Self { |
| 25 | + ListenerCell { |
| 26 | + rc: Rc::new(RefCell::new(listener)), |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /// This is used by `Subject` to obtain the listener. |
| 31 | + pub fn get_ref(&self) -> ListenerRef { |
| 32 | + let rc: Rc<RefCell<dyn ListenerUpdate>> = self.rc.clone(); |
| 33 | + ListenerRef { |
| 34 | + weak: Rc::downgrade(&rc), |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + pub fn as_ref(&self) -> Ref<'_, T> { |
| 39 | + self.rc.borrow() |
| 40 | + } |
| 41 | + |
| 42 | + pub fn as_mut(&mut self) -> RefMut<'_, T> { |
| 43 | + self.rc.borrow_mut() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl<T> Clone for ListenerCell<T> { |
| 48 | + fn clone(&self) -> Self { |
| 49 | + ListenerCell { |
| 50 | + rc: self.rc.clone(), |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Eq)] |
| 56 | +pub struct ListenerRef { |
| 57 | + weak: Weak<RefCell<dyn ListenerUpdate>>, |
| 58 | +} |
| 59 | + |
| 60 | +pub enum ListenerError { |
| 61 | + Invalid, |
| 62 | +} |
| 63 | + |
| 64 | +impl ListenerRef { |
| 65 | + #[inline] |
| 66 | + pub fn is_valid(&self) -> bool { |
| 67 | + self.weak.upgrade().is_some() |
| 68 | + } |
| 69 | + |
| 70 | + pub fn try_update(&mut self, event: &EditorEvent) -> Result<(), ListenerError> { |
| 71 | + if let Some(rc) = self.weak.upgrade() { |
| 72 | + rc.borrow_mut().update(event); |
| 73 | + Ok(()) |
| 74 | + } else { |
| 75 | + Err(ListenerError::Invalid) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + pub fn update(&mut self, event: &EditorEvent) { |
| 80 | + match self.try_update(event) { |
| 81 | + Ok(_) => (), |
| 82 | + Err(ListenerError::Invalid) => panic!("Listener doesn't exist anymore"), |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl PartialEq for ListenerRef { |
| 88 | + fn eq(&self, other: &Self) -> bool { |
| 89 | + self.weak.ptr_eq(&other.weak) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[cfg(test)] |
| 94 | +mod tests { |
| 95 | + use crate::observer::email::EmailAlerts; |
| 96 | + use crate::observer::subject::Subject; |
| 97 | + |
| 98 | + use super::*; |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn listener_drop() { |
| 102 | + let listener = EmailAlerts::new(); |
| 103 | + let mut subject = Subject::new(); |
| 104 | + { |
| 105 | + let listener = ListenerCell::from(listener); |
| 106 | + assert_eq!(Rc::weak_count(&listener.rc), 0); |
| 107 | + assert_eq!(Rc::strong_count(&listener.rc), 1); |
| 108 | + subject.add_listener(&listener); |
| 109 | + subject.add_listener(&listener); |
| 110 | + assert_eq!(subject.listener_count(), 2); |
| 111 | + assert_eq!(Rc::weak_count(&listener.rc), 2); |
| 112 | + assert_eq!(Rc::strong_count(&listener.rc), 1); |
| 113 | + } |
| 114 | + assert_eq!(subject.listener_count(), 0); |
| 115 | + } |
| 116 | +} |
0 commit comments