mod beckon;
mod gather;
mod hover;
mod mark;
mod match_;
mod search;
mod yield_;
pub use beckon::BeckonStateUnit;
pub use gather::GatherStateUnit;
pub use hover::HoverDwell;
pub use mark::{MarkStateUnit, PendingAnnotation};
pub use match_::MatchStateUnit;
pub use search::{SearchPipeline, SearchPipelineOptions, SearchState};
pub use yield_::YieldStateUnit;
use crate::bus::decoded;
use crate::channels::Channel;
use crate::client::SemiontClient;
use crate::errors::{JobErrorCode, SemiontError};
use crate::locked;
use crate::transport::{Envelope, Frame};
use std::collections::HashMap;
use std::future::Future;
use std::sync::Mutex;
use tokio::sync::watch;
use tokio::task::{self, AbortHandle, JoinSet};
pub(crate) struct Held<T> {
sender: Mutex<Option<watch::Sender<T>>>,
reader: watch::Receiver<T>,
}
impl<T> Held<T> {
pub fn new(initial: T) -> Held<T> {
Held::of(watch::channel(initial).0)
}
pub fn of(sender: watch::Sender<T>) -> Held<T> {
let reader = sender.subscribe();
Held {
sender: Mutex::new(Some(sender)),
reader,
}
}
pub fn read(&self) -> watch::Receiver<T> {
match locked(&self.sender).as_ref() {
Some(sender) => sender.subscribe(),
None => self.reader.clone(),
}
}
pub fn raise(&self, value: T) {
if let Some(sender) = locked(&self.sender).as_ref() {
sender.send_replace(value);
}
}
pub fn end(&self) {
*locked(&self.sender) = None;
}
}
impl<T: Clone> Held<T> {
pub fn now(&self) -> T {
self.reader.borrow().clone()
}
}
impl<T: PartialEq> Held<T> {
pub fn set(&self, value: T) {
if let Some(sender) = locked(&self.sender).as_ref() {
sender.send_if_modified(|held| {
let changed = *held != value;
if changed {
*held = value;
}
changed
});
}
}
}
impl<T: PartialEq> Held<Option<T>> {
pub fn forget(&self, value: &T) {
if let Some(sender) = locked(&self.sender).as_ref() {
sender.send_if_modified(|held| {
let is_it = held.as_ref() == Some(value);
if is_it {
*held = None;
}
is_it
});
}
}
}
pub(crate) struct Tasks {
running: Mutex<Option<Running>>,
}
struct Running {
tasks: JoinSet<()>,
handles: HashMap<task::Id, AbortHandle>,
}
impl Tasks {
pub fn new() -> Tasks {
Tasks {
running: Mutex::new(Some(Running {
tasks: JoinSet::new(),
handles: HashMap::new(),
})),
}
}
pub fn spawn(&self, work: impl Future<Output = ()> + Send + 'static) {
if let Some(running) = locked(&self.running).as_mut() {
while let Some(finished) = running.tasks.try_join_next_with_id() {
let id = finished.map_or_else(|ended| ended.id(), |(id, ())| id);
running.handles.remove(&id);
}
let handle = running.tasks.spawn(work);
running.handles.insert(handle.id(), handle);
}
}
pub fn stopped(&self) -> bool {
locked(&self.running).is_none()
}
pub fn stop(&self) {
*locked(&self.running) = None;
}
pub async fn stop_and_wait(&self) -> Option<AbortHandle> {
let mut running = locked(&self.running).take()?;
let own = task::try_id().and_then(|id| running.handles.remove(&id));
for handle in running.handles.values() {
handle.abort();
}
while running.tasks.len() > usize::from(own.is_some()) {
running.tasks.join_next().await;
}
running.tasks.detach_all();
own
}
}
pub(crate) fn said<C: Channel>(frame: &Frame) -> Option<C::Payload> {
if frame.channel != C::NAME {
return None;
}
decoded::<C>(frame.payload.clone()).ok()
}
pub(crate) fn signal<C: Channel>(client: &SemiontClient, payload: &C::Payload, envelope: Envelope) {
let _ = client.bus().publish::<C>(payload, envelope);
}
pub(crate) fn cancelled(error: &SemiontError) -> bool {
matches!(error, SemiontError::Job(job) if job.code == JobErrorCode::Cancelled)
}