use super::{Held, Tasks, said, signal};
use crate::channels::{BeckonFocus, BeckonHover, BrowseClick, Channel};
use crate::client::SemiontClient;
use crate::event_bus::BusFrames;
use crate::state_unit::StateUnit;
use crate::transport::Envelope;
use crate::types::BeckonFocusEvent;
use crate::types::{AnnotationId, ResourceId};
use std::sync::Arc;
use tokio::sync::watch;
struct Shared {
client: Arc<SemiontClient>,
resource_id: ResourceId,
hovered: Held<Option<AnnotationId>>,
tasks: Tasks,
}
impl Shared {
fn focus(&self, annotation_id: AnnotationId) {
signal::<BeckonFocus>(
&self.client,
&BeckonFocusEvent {
annotation_id: Some(annotation_id),
resource_id: self.resource_id.clone(),
},
Envelope::default(),
);
}
}
pub struct BeckonStateUnit {
shared: Arc<Shared>,
}
impl BeckonStateUnit {
pub fn new(client: Arc<SemiontClient>, resource_id: &ResourceId) -> BeckonStateUnit {
let heard = client
.bus()
.frames_among(&[BeckonHover::NAME, BrowseClick::NAME]);
let shared = Arc::new(Shared {
client,
resource_id: resource_id.clone(),
hovered: Held::new(None),
tasks: Tasks::new(),
});
shared.tasks.spawn(listen(shared.clone(), heard));
BeckonStateUnit { shared }
}
pub fn hovered_annotation_id(&self) -> watch::Receiver<Option<AnnotationId>> {
self.shared.hovered.read()
}
pub fn focus(&self, annotation_id: &AnnotationId) {
if !self.shared.tasks.stopped() {
self.shared.focus(annotation_id.clone());
}
}
}
async fn listen(shared: Arc<Shared>, mut heard: BusFrames) {
while let Some(frame) = heard.next().await {
let Ok(Ok(frame)) = frame else { continue };
if let Some(hover) = said::<BeckonHover>(&frame) {
shared.hovered.set(hover.annotation_id.clone());
if let Some(annotation_id) = hover.annotation_id {
shared.client.beckon.sparkle(&annotation_id);
}
} else if let Some(click) = said::<BrowseClick>(&frame) {
shared.focus(click.annotation_id);
}
}
}
impl StateUnit for BeckonStateUnit {
fn dispose(&self) {
self.shared.tasks.stop();
self.shared.hovered.end();
}
}
impl Drop for BeckonStateUnit {
fn drop(&mut self) {
self.dispose();
}
}