Skip to content

Commit

Permalink
Deduplicate subscription code into EventSubscribers::subscribe_recipi…
Browse files Browse the repository at this point in the history
…ent()
  • Loading branch information
strohel committed Aug 8, 2024
1 parent 7ec1f87 commit 789611c
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,19 @@ struct EventSubscribers {
last_value_cache: HashMap<TypeId, Box<dyn std::any::Any + Send + Sync>>,
}

impl EventSubscribers {
fn subscribe_recipient<M: 'static, E: Event + Into<M>>(&mut self, recipient: Recipient<M>) {
let subs = self.events.entry(TypeId::of::<E>()).or_default();
subs.push(Box::new(move |e| {
if let Some(event) = e.downcast_ref::<E>() {
let msg = event.clone();
recipient.send(msg.into())?;
}
Ok(())
}));
}
}

/// Contains the "metadata" of the system, including information about the registry
/// of actors currently existing within the system.
#[derive(Default, Clone)]
Expand Down Expand Up @@ -747,17 +760,7 @@ impl SystemHandle {
/// Subscribe given `recipient` to events of type `E`. See [`Context::subscribe()`].
pub fn subscribe_recipient<M: 'static, E: Event + Into<M>>(&self, recipient: Recipient<M>) {
let mut event_subscribers = self.event_subscribers.write();

let subs = event_subscribers.events.entry(TypeId::of::<E>()).or_default();

subs.push(Box::new(move |e| {
if let Some(event) = e.downcast_ref::<E>() {
let msg = event.clone();
recipient.send(msg.into())?;
}

Ok(())
}));
event_subscribers.subscribe_recipient::<M, E>(recipient);
}

/// Subscribe given `recipient` to events of type `E` and send the last cached event to it.
Expand All @@ -776,18 +779,7 @@ impl SystemHandle {
}
}

// Duplicate the contents of Self::subscribe_recipient() to reuse the same WriteGuard
// for better performance.
let subs = event_subscribers.events.entry(TypeId::of::<E>()).or_default();
subs.push(Box::new(move |e| {
if let Some(event) = e.downcast_ref::<E>() {
let msg = event.clone();
recipient.send(msg.into())?;
}

Ok(())
}));

event_subscribers.subscribe_recipient::<M, E>(recipient);
Ok(())
}

Expand Down

0 comments on commit 789611c

Please sign in to comment.