use std::sync::{Arc, RwLock};
use reovim_kernel::api::v1::Service;
use crate::CommandHandler;
pub struct CommandHandlerStore {
handlers: RwLock<Vec<Arc<dyn CommandHandler>>>,
}
impl CommandHandlerStore {
#[must_use]
#[allow(clippy::missing_const_for_fn)] pub fn new() -> Self {
Self {
handlers: RwLock::new(Vec::new()),
}
}
pub fn add(&self, handler: Box<dyn CommandHandler>) {
self.handlers
.write()
.expect("CommandHandlerStore lock poisoned")
.push(handler.into());
}
pub fn add_arc(&self, handler: Arc<dyn CommandHandler>) {
self.handlers
.write()
.expect("CommandHandlerStore lock poisoned")
.push(handler);
}
pub fn take_handlers(&self) -> Vec<Arc<dyn CommandHandler>> {
std::mem::take(
&mut *self
.handlers
.write()
.expect("CommandHandlerStore lock poisoned"),
)
}
#[must_use]
pub fn len(&self) -> usize {
self.handlers
.read()
.expect("CommandHandlerStore lock poisoned")
.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.handlers
.read()
.expect("CommandHandlerStore lock poisoned")
.is_empty()
}
}
impl Default for CommandHandlerStore {
fn default() -> Self {
Self::new()
}
}
impl Service for CommandHandlerStore {}
impl std::fmt::Debug for CommandHandlerStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CommandHandlerStore")
.field("count", &self.len())
.finish()
}
}
#[cfg(test)]
#[path = "registry_tests.rs"]
mod tests;