#![allow(clippy::type_complexity, clippy::significant_drop_tightening)]
use std::sync::Arc;
use tokio::sync::RwLock;
use crate::process_registry::{ProcessRecord, ProcessRegistry};
#[derive(Debug, Clone)]
pub struct ProcessVisibilityScope {
pub own: Arc<RwLock<ProcessRegistry>>,
children: Arc<tokio::sync::Mutex<Vec<(String, Arc<RwLock<ProcessRegistry>>)>>>,
}
impl ProcessVisibilityScope {
#[must_use]
pub fn new(registry: Arc<RwLock<ProcessRegistry>>) -> Self {
Self {
own: registry,
children: Arc::new(tokio::sync::Mutex::new(Vec::new())),
}
}
pub async fn add_child_registry(
&self,
label: impl Into<String>,
registry: Arc<RwLock<ProcessRegistry>>,
) {
self.children.lock().await.push((label.into(), registry));
}
pub async fn visible_running(&self) -> Vec<(Option<String>, ProcessRecord)> {
let mut result = Vec::new();
{
let own = self.own.read().await;
for r in own.running() {
result.push((None, r.clone()));
}
}
let children = self.children.lock().await;
for (label, reg) in children.iter() {
let child_reg = reg.read().await;
for r in child_reg.running() {
result.push((Some(label.clone()), r.clone()));
}
}
result
}
pub async fn find(&self, pid: u32) -> Option<(Option<String>, ProcessRecord)> {
{
let own = self.own.read().await;
if let Some(r) = own.get(pid) {
return Some((None, r.clone()));
}
}
let children = self.children.lock().await;
for (label, reg) in children.iter() {
let child_reg = reg.read().await;
if let Some(r) = child_reg.get(pid) {
return Some((Some(label.clone()), r.clone()));
}
}
None
}
}