use crate::bus::{Typed, payload_of};
use crate::channels::Channel;
use crate::errors::TransportError;
use crate::transport::{Broadcast, Envelope, Events, Frame, FrameRefusal, Lagged, STREAM_BACKLOG};
use crate::types::ResourceId;
use futures_core::Stream;
use serde_json::{Map, Value};
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::{Arc, Mutex, MutexGuard};
use std::task::{Context, Poll};
use tokio::sync::broadcast;
type View = (String, Option<ResourceId>);
struct Channels {
frames: HashMap<String, Broadcast>,
among: Vec<(Vec<String>, Broadcast)>,
observers: HashMap<View, usize>,
}
struct Inner {
channels: Mutex<Option<Channels>>,
}
impl Inner {
fn channels(&self) -> MutexGuard<'_, Option<Channels>> {
self.channels
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
fn deliver(&self, carried: Result<Frame, FrameRefusal>) -> usize {
let (channel, scope) = match &carried {
Ok(frame) => (&frame.channel, &frame.scope),
Err(refusal) => (&refusal.channel, &refusal.scope),
};
let mut channels = self.channels();
let Some(channels) = channels.as_mut() else {
return 0;
};
let observers = channels
.observers
.get(&(channel.clone(), scope.clone()))
.copied()
.unwrap_or(0);
for (seen, sender) in &channels.among {
if seen.iter().any(|one| one == channel) {
let _ = sender.send(carried.clone());
}
}
if let Some(sender) = channels.frames.get(channel) {
let _ = sender.send(carried);
}
observers
}
fn frames(self: &Arc<Self>, channel: &str, scope: Option<ResourceId>) -> BusFrames {
let views = vec![(channel.to_owned(), scope.clone())];
let mut channels = self.channels();
let events = match channels.as_mut() {
Some(channels) => {
*channels.observers.entry(views[0].clone()).or_insert(0) += 1;
Events::new(
channels
.frames
.entry(channel.to_owned())
.or_insert_with(|| broadcast::channel(STREAM_BACKLOG).0)
.subscribe(),
)
}
None => Events::new(broadcast::channel(1).1),
};
BusFrames {
bus: self.clone(),
views,
scope,
events,
}
}
fn frames_among(self: &Arc<Self>, among: &[&str]) -> BusFrames {
let seen: Vec<String> = among.iter().map(|channel| (*channel).to_owned()).collect();
let views: Vec<View> = seen.iter().map(|channel| (channel.clone(), None)).collect();
let mut channels = self.channels();
let events = match channels.as_mut() {
Some(channels) => {
for view in &views {
*channels.observers.entry(view.clone()).or_insert(0) += 1;
}
channels
.among
.retain(|(_, sender)| sender.receiver_count() > 0);
let (sender, receiver) = broadcast::channel(STREAM_BACKLOG);
channels.among.push((seen, sender));
Events::new(receiver)
}
None => Events::new(broadcast::channel(1).1),
};
BusFrames {
bus: self.clone(),
views,
scope: None,
events,
}
}
}
pub struct BusFrames {
bus: Arc<Inner>,
views: Vec<View>,
scope: Option<ResourceId>,
events: Events<Result<Frame, FrameRefusal>>,
}
impl BusFrames {
pub async fn next(&mut self) -> Option<Result<Result<Frame, FrameRefusal>, Lagged>> {
std::future::poll_fn(|cx| Pin::new(&mut *self).poll_next(cx)).await
}
}
impl Stream for BusFrames {
type Item = Result<Result<Frame, FrameRefusal>, Lagged>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
loop {
match Pin::new(&mut self.events).poll_next(cx) {
Poll::Ready(Some(Ok(Ok(frame)))) if frame.scope != self.scope => continue,
Poll::Ready(Some(Ok(Err(refusal)))) if refusal.scope != self.scope => continue,
other => return other,
}
}
}
}
impl Drop for BusFrames {
fn drop(&mut self) {
let mut channels = self.bus.channels();
let Some(channels) = channels.as_mut() else {
return;
};
for view in &self.views {
if let Some(count) = channels.observers.get_mut(view) {
*count = count.saturating_sub(1);
if *count == 0 {
channels.observers.remove(view);
}
}
}
}
}
#[derive(Clone)]
pub struct EventBus {
inner: Arc<Inner>,
}
impl Default for EventBus {
fn default() -> EventBus {
EventBus {
inner: Arc::new(Inner {
channels: Mutex::new(Some(Channels {
frames: HashMap::new(),
among: Vec::new(),
observers: HashMap::new(),
})),
}),
}
}
}
impl EventBus {
pub fn new() -> EventBus {
EventBus::default()
}
pub fn emit(&self, channel: &str, payload: Map<String, Value>, envelope: Envelope) -> usize {
self.inner.deliver(Ok(Frame {
channel: channel.to_owned(),
payload,
correlation_id: envelope.correlation_id,
scope: envelope.scope,
trace: None,
}))
}
pub fn emit_refusal(&self, refusal: FrameRefusal) -> usize {
self.inner.deliver(Err(refusal))
}
pub fn frames(&self, channel: &str) -> BusFrames {
self.inner.frames(channel, None)
}
pub fn frames_among(&self, channels: &[&str]) -> BusFrames {
self.inner.frames_among(channels)
}
pub fn publish<C: Channel>(
&self,
payload: &C::Payload,
envelope: Envelope,
) -> Result<usize, TransportError> {
Ok(self.emit(C::NAME, payload_of(payload)?, envelope))
}
pub fn stream<C: Channel>(&self) -> Typed<C, BusFrames> {
Typed::of(self.frames(C::NAME))
}
pub fn scope(&self, resource_id: &ResourceId) -> ScopedEventBus {
ScopedEventBus {
inner: self.inner.clone(),
scope: resource_id.clone(),
}
}
pub fn observed_channels(&self) -> Vec<String> {
let mut observed: Vec<String> = self
.inner
.channels()
.as_ref()
.map(|channels| channels.observers.keys().map(|(c, _)| c.clone()).collect())
.unwrap_or_default();
observed.sort();
observed.dedup();
observed
}
pub fn destroy(&self) {
*self.inner.channels() = None;
}
pub fn destroyed(&self) -> bool {
self.inner.channels().is_none()
}
}
#[derive(Clone)]
pub struct ScopedEventBus {
inner: Arc<Inner>,
scope: ResourceId,
}
impl ScopedEventBus {
pub fn emit(
&self,
channel: &str,
payload: Map<String, Value>,
correlation_id: Option<String>,
) -> usize {
self.inner.deliver(Ok(Frame {
channel: channel.to_owned(),
payload,
correlation_id,
scope: Some(self.scope.clone()),
trace: None,
}))
}
pub fn frames(&self, channel: &str) -> BusFrames {
self.inner.frames(channel, Some(self.scope.clone()))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::transport::FrameRefusalReason;
use serde_json::json;
fn payload(n: u64) -> Map<String, Value> {
json!({ "n": n }).as_object().cloned().unwrap_or_default()
}
fn focus(n: u64) -> Map<String, Value> {
let mut focus = payload(n);
focus.insert("resourceId".to_owned(), json!("res-1"));
focus
}
#[tokio::test]
async fn a_global_view_does_not_see_a_scope_and_a_scope_sees_only_its_own() {
let bus = EventBus::new();
let mut global = bus.frames("mark:added");
let mut one = bus.scope(&"res-1".parse().unwrap()).frames("mark:added");
let mut other = bus.scope(&"res-2".parse().unwrap()).frames("mark:added");
assert_eq!(bus.emit("mark:added", payload(1), Envelope::default()), 1);
assert_eq!(
bus.scope(&"res-1".parse().unwrap())
.emit("mark:added", payload(2), None),
1
);
assert_eq!(
bus.scope(&"res-3".parse().unwrap())
.emit("mark:added", payload(3), None),
0
);
bus.destroy();
let seen = |frames: Vec<Frame>| -> Vec<Value> {
frames.into_iter().map(|f| f.payload["n"].clone()).collect()
};
let mut drained = Vec::new();
while let Some(Ok(Ok(frame))) = global.next().await {
drained.push(frame);
}
assert_eq!(seen(drained), [json!(1)]);
let mut drained = Vec::new();
while let Some(Ok(Ok(frame))) = one.next().await {
drained.push(frame);
}
assert_eq!(seen(drained), [json!(2)]);
assert_eq!(other.next().await, None);
}
#[tokio::test]
async fn the_count_follows_the_readers() {
let bus = EventBus::new();
assert_eq!(bus.emit("beckon:focus", focus(1), Envelope::default()), 0);
let first = bus.frames("beckon:focus");
let second = bus.frames("beckon:focus");
assert_eq!(bus.emit("beckon:focus", focus(2), Envelope::default()), 2);
assert_eq!(bus.observed_channels(), ["beckon:focus"]);
drop(first);
drop(second);
assert_eq!(bus.emit("beckon:focus", focus(3), Envelope::default()), 0);
assert!(bus.observed_channels().is_empty());
}
#[tokio::test]
async fn a_view_of_several_channels_gives_them_in_the_order_they_were_published() {
let bus = EventBus::new();
let mut both = bus.frames_among(&["mark:requested", "mark:submit"]);
assert_eq!(bus.observed_channels(), ["mark:requested", "mark:submit"]);
for n in 0..6 {
let channel = if n % 2 == 0 {
"mark:requested"
} else {
"mark:submit"
};
assert_eq!(bus.emit(channel, payload(n), Envelope::default()), 1);
}
bus.emit("mark:create-error", payload(9), Envelope::default());
bus.scope(&"res-1".parse().unwrap())
.emit("mark:requested", payload(9), None);
drop(bus.frames_among(&["mark:requested"]));
bus.destroy();
let mut seen = Vec::new();
while let Some(Ok(Ok(frame))) = both.next().await {
seen.push((frame.channel, frame.payload["n"].clone()));
}
let said = |channel: &str, n: u64| (channel.to_owned(), json!(n));
assert_eq!(
seen,
[
said("mark:requested", 0),
said("mark:submit", 1),
said("mark:requested", 2),
said("mark:submit", 3),
said("mark:requested", 4),
said("mark:submit", 5),
]
);
}
#[tokio::test]
async fn a_view_of_several_channels_is_counted_on_each_until_it_is_dropped() {
let bus = EventBus::new();
let both = bus.frames_among(&["beckon:hover", "browse:click"]);
assert_eq!(bus.emit("beckon:hover", payload(1), Envelope::default()), 1);
assert_eq!(bus.emit("browse:click", payload(2), Envelope::default()), 1);
drop(both);
assert_eq!(bus.emit("beckon:hover", payload(3), Envelope::default()), 0);
assert!(bus.observed_channels().is_empty());
}
#[tokio::test]
async fn a_destroyed_bus_is_inert() {
let bus = EventBus::new();
bus.destroy();
bus.destroy();
assert!(bus.destroyed());
assert_eq!(bus.emit("beckon:focus", focus(1), Envelope::default()), 0);
assert_eq!(bus.frames("beckon:focus").next().await, None);
assert_eq!(bus.frames_among(&["beckon:focus"]).next().await, None);
}
#[tokio::test]
async fn a_refusal_is_published_into_the_view_its_channel_and_scope_name() {
let bus = EventBus::new();
let resource: ResourceId = "res-1".parse().unwrap();
let mut global = bus.frames("beckon:focus");
let mut scoped = bus.scope(&resource).frames("beckon:focus");
let mut among = bus.frames_among(&["beckon:focus"]);
let refusal = |scope: Option<ResourceId>| FrameRefusal {
channel: "beckon:focus".to_owned(),
member: "correlationId".to_owned(),
reason: FrameRefusalReason::Malformed,
payload: Some(Value::Object(focus(1))),
scope,
correlation_id: None,
};
assert_eq!(bus.emit_refusal(refusal(None)), 2);
assert_eq!(bus.emit_refusal(refusal(Some(resource.clone()))), 1);
bus.destroy();
assert_eq!(global.next().await, Some(Ok(Err(refusal(None)))));
assert_eq!(global.next().await, None);
assert_eq!(among.next().await, Some(Ok(Err(refusal(None)))));
assert_eq!(among.next().await, None);
assert_eq!(scoped.next().await, Some(Ok(Err(refusal(Some(resource))))));
assert_eq!(scoped.next().await, None);
}
}