reifydb-transaction 0.6.0

Transaction management and concurrency control for ReifyDB
Documentation
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (c) 2026 ReifyDB

use std::sync::Arc;

use reifydb_runtime::sync::rwlock::RwLock;

use crate::interceptor::interceptors::Interceptors;

type InterceptorFactoryFn = Box<dyn Fn(&mut Interceptors) + Send + Sync>;
type LateInterceptorFactoryFn = Arc<dyn Fn(&mut Interceptors) + Send + Sync>;

pub struct InterceptorFactory {
	pub(crate) factories: Vec<InterceptorFactoryFn>,
	late: RwLock<Vec<LateInterceptorFactoryFn>>,
}

impl Default for InterceptorFactory {
	fn default() -> Self {
		Self {
			factories: Vec::new(),
			late: RwLock::new(Vec::new()),
		}
	}
}

impl InterceptorFactory {
	pub fn add(&mut self, factory: InterceptorFactoryFn) {
		self.factories.push(factory);
	}

	pub fn add_late(&self, factory: LateInterceptorFactoryFn) {
		self.late.write().push(factory);
	}

	pub fn clear_late(&self) {
		self.late.write().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().iter() {
			factory(&mut interceptors);
		}

		interceptors
	}
}