jj_store.rs

 1use std::path::Path;
 2use std::sync::Arc;
 3
 4use gpui::{App, Entity, Global, prelude::*};
 5
 6use crate::{JujutsuRepository, RealJujutsuRepository};
 7
 8/// Note: We won't ultimately be storing the jj store in a global, we're just doing this for exploration purposes.
 9struct GlobalJujutsuStore(Entity<JujutsuStore>);
10
11impl Global for GlobalJujutsuStore {}
12
13pub struct JujutsuStore {
14    repository: Arc<dyn JujutsuRepository>,
15}
16
17impl JujutsuStore {
18    pub fn init_global(cx: &mut App) {
19        let Some(repository) = RealJujutsuRepository::new(Path::new(".")).ok() else {
20            return;
21        };
22
23        let repository = Arc::new(repository);
24        let jj_store = cx.new(|cx| JujutsuStore::new(repository, cx));
25
26        cx.set_global(GlobalJujutsuStore(jj_store));
27    }
28
29    pub fn try_global(cx: &App) -> Option<Entity<Self>> {
30        cx.try_global::<GlobalJujutsuStore>()
31            .map(|global| global.0.clone())
32    }
33
34    pub fn new(repository: Arc<dyn JujutsuRepository>, _cx: &mut Context<Self>) -> Self {
35        Self { repository }
36    }
37
38    pub fn repository(&self) -> &Arc<dyn JujutsuRepository> {
39        &self.repository
40    }
41}