use std::sync::{Arc, RwLock};
use crate::interceptor::interceptors::Interceptors;
pub struct InterceptorFactory {
pub(crate) factories: Vec<Box<dyn Fn(&mut Interceptors) + Send + Sync>>,
late: RwLock<Vec<Arc<dyn Fn(&mut Interceptors) + Send + Sync>>>,
}
impl Default for InterceptorFactory {
fn default() -> Self {
Self {
factories: Vec::new(),
late: RwLock::new(Vec::new()),
}
}
}
impl InterceptorFactory {
pub fn add(&mut self, factory: Box<dyn Fn(&mut Interceptors) + Send + Sync>) {
self.factories.push(factory);
}
pub fn add_late(&self, factory: Arc<dyn Fn(&mut Interceptors) + Send + Sync>) {
self.late.write().unwrap().push(factory);
}
pub fn clear_late(&self) {
self.late.write().unwrap().clear();
}
pub fn create(&self) -> Interceptors {
let mut interceptors = Interceptors::new();
for factory in &self.factories {
factory(&mut interceptors);
}
for factory in self.late.read().unwrap().iter() {
factory(&mut interceptors);
}
interceptors
}
}