Skip to main content

aft/
runtime_registry.rs

1use std::sync::Arc;
2
3use crate::context::{App, AppContext};
4
5/// Standalone and subc contexts share the same owned handle so deferred work
6/// can outlive the synchronous dispatch stack without borrowing the runtime.
7pub type ProjectRuntime = Arc<AppContext>;
8
9pub struct RuntimeRegistry {
10    app: Arc<App>,
11    single: ProjectRuntime,
12}
13
14impl RuntimeRegistry {
15    pub fn standalone(app: Arc<App>, rt: ProjectRuntime) -> Self {
16        Self { app, single: rt }
17    }
18
19    pub fn app(&self) -> Arc<App> {
20        Arc::clone(&self.app)
21    }
22
23    pub fn current(&self) -> &ProjectRuntime {
24        &self.single
25    }
26
27    pub fn current_mut(&mut self) -> &mut ProjectRuntime {
28        &mut self.single
29    }
30
31    pub fn iter(&self) -> impl Iterator<Item = &ProjectRuntime> {
32        std::iter::once(&self.single)
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use crate::{config::Config, parser::TreeSitterProvider};
40
41    #[test]
42    fn standalone_current_and_iter_return_single_runtime() {
43        let ctx = Arc::new(AppContext::new(
44            Box::new(TreeSitterProvider::new()),
45            Config::default(),
46        ));
47        let app = ctx.app();
48        let mut registry = RuntimeRegistry::standalone(Arc::clone(&app), ctx);
49        assert!(Arc::ptr_eq(&app, &registry.app()));
50        assert!(Arc::ptr_eq(&app, &registry.current().app()));
51
52        let current_ptr = Arc::as_ptr(registry.current());
53        let iter_ptrs = registry.iter().map(Arc::as_ptr).collect::<Vec<_>>();
54        assert_eq!(iter_ptrs, vec![current_ptr]);
55
56        let current_mut_ptr = Arc::as_ptr(registry.current_mut());
57        assert_eq!(current_mut_ptr, current_ptr);
58    }
59}