use std::{sync::Arc, time::Duration};
use {parking_lot::Mutex, reovim_kernel::api::v1::Service};
use crate::ClientId;
pub trait TickScheduler: Send + Sync + 'static {
fn start(&self, client_id: ClientId, kind: &'static str, interval: Duration);
fn stop(&self, client_id: ClientId, kind: &'static str);
fn pause(&self, client_id: ClientId, kind: &'static str);
fn resume(&self, client_id: ClientId, kind: &'static str);
}
pub struct TickSchedulerHandle {
inner: Mutex<Option<Arc<dyn TickScheduler>>>,
}
impl Default for TickSchedulerHandle {
fn default() -> Self {
Self {
inner: Mutex::new(None),
}
}
}
impl Service for TickSchedulerHandle {}
impl TickSchedulerHandle {
pub fn set(&self, scheduler: Arc<dyn TickScheduler>) {
*self.inner.lock() = Some(scheduler);
}
pub fn start(&self, client_id: ClientId, kind: &'static str, interval: Duration) {
if let Some(ref s) = *self.inner.lock() {
s.start(client_id, kind, interval);
}
}
pub fn stop(&self, client_id: ClientId, kind: &'static str) {
if let Some(ref s) = *self.inner.lock() {
s.stop(client_id, kind);
}
}
pub fn pause(&self, client_id: ClientId, kind: &'static str) {
if let Some(ref s) = *self.inner.lock() {
s.pause(client_id, kind);
}
}
pub fn resume(&self, client_id: ClientId, kind: &'static str) {
if let Some(ref s) = *self.inner.lock() {
s.resume(client_id, kind);
}
}
}
impl std::fmt::Debug for TickSchedulerHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let has_scheduler = self.inner.lock().is_some();
f.debug_struct("TickSchedulerHandle")
.field("has_scheduler", &has_scheduler)
.finish()
}
}
#[cfg(test)]
#[path = "tick_tests.rs"]
mod tests;