use std::sync::Arc;
use crate::events::CacheEvent;
pub trait Plugin: Send + Sync {
fn name(&self) -> &str;
fn on_start(&self) {}
fn on_event(&self, event: &CacheEvent);
}
#[derive(Clone, Default)]
pub struct PluginHost {
plugins: Arc<[Arc<dyn Plugin>]>,
}
impl PluginHost {
#[must_use]
pub fn new(plugins: Vec<Arc<dyn Plugin>>) -> Self {
for plugin in &plugins {
plugin.on_start();
}
Self {
plugins: plugins.into(),
}
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.plugins.is_empty()
}
pub fn notify(&self, event: &CacheEvent) {
for plugin in self.plugins.iter() {
plugin.on_event(event);
}
}
}
impl std::fmt::Debug for PluginHost {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PluginHost")
.field("count", &self.plugins.len())
.finish()
}
}