summaryrefslogtreecommitdiff
path: root/src/broadcast.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/broadcast.rs')
-rw-r--r--src/broadcast.rs22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/broadcast.rs b/src/broadcast.rs
index 6e1f04d..ee42c08 100644
--- a/src/broadcast.rs
+++ b/src/broadcast.rs
@@ -30,7 +30,7 @@ impl<M> Broadcaster<M>
where
M: Clone + Send + std::fmt::Debug + 'static,
{
- pub fn broadcast(&self, message: impl Into<M>) {
+ pub fn broadcast(&self, message: M) {
let tx = self.sender();
// Per the Tokio docs, the returned error is only used to indicate that
@@ -40,7 +40,25 @@ where
//
// The successful return value, which includes the number of active
// receivers, also isn't that interesting to us.
- let _ = tx.send(message.into());
+ let _ = tx.send(message);
+ }
+
+ // If `M` is a type that can be obtained from an iterator, such as a `Vec`, and if `I` is an
+ // iterable of items that can be collected into `M`, then this will construct an `M` from the
+ // passed event iterator, converting each element as it goes. This emits one message (as `M`),
+ // containing whatever we collect out of `messages`.
+ //
+ // This is mostly meant for handling synchronized entity events, which tend to be generated as
+ // iterables of domain-specific event types, like `user::Event`, but broadcast as `Vec<event::Event>`
+ // for consumption by outside clients.
+ pub fn broadcast_from<I, E>(&self, messages: I)
+ where
+ I: IntoIterator,
+ M: FromIterator<E>,
+ E: From<I::Item>,
+ {
+ let message = messages.into_iter().map(Into::into).collect();
+ self.broadcast(message);
}
pub fn subscribe(&self) -> impl Stream<Item = M> + std::fmt::Debug + use<M> {