2026-04-20 15:13:26 +01:00
|
|
|
use gpui::{AppContext, BorrowAppContext};
|
2026-04-21 20:30:41 +01:00
|
|
|
use std::{any::Any, collections::HashMap, marker::PhantomData};
|
2026-04-20 15:13:26 +01:00
|
|
|
|
2026-04-21 20:30:41 +01:00
|
|
|
pub trait QueryFn: Clone + 'static {
|
2026-04-20 15:13:26 +01:00
|
|
|
type Data: 'static;
|
|
|
|
|
type Error: 'static;
|
2026-04-21 20:30:41 +01:00
|
|
|
type Context: Context;
|
2026-04-20 15:13:26 +01:00
|
|
|
|
|
|
|
|
fn key(&self) -> &'static str;
|
|
|
|
|
|
2026-04-21 20:30:41 +01:00
|
|
|
async fn run(&self, c: &Self::Context) -> Result<Self::Data, Self::Error>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Query {
|
|
|
|
|
key: &'static str,
|
|
|
|
|
data: QueryData,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum QueryData {
|
|
|
|
|
Pending,
|
|
|
|
|
Loading,
|
|
|
|
|
Stale,
|
|
|
|
|
Some(Box<dyn Any>),
|
|
|
|
|
Err(Box<dyn Any>),
|
2026-04-20 15:13:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub enum QueryStatus<'a, Data, Error> {
|
|
|
|
|
Loading,
|
|
|
|
|
Loaded(&'a Data),
|
|
|
|
|
Err(&'a Error),
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 20:30:41 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub(crate) struct Entity<F>
|
2026-04-20 15:13:26 +01:00
|
|
|
where
|
2026-04-21 20:30:41 +01:00
|
|
|
F: QueryFn,
|
|
|
|
|
{
|
|
|
|
|
raw: gpui::Entity<Query>,
|
|
|
|
|
_marker: PhantomData<fn() -> F>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn use_query<F, T>(query_fn: F, cx: &mut gpui::Context<T>) -> Entity<F>
|
|
|
|
|
where
|
|
|
|
|
F: QueryFn,
|
2026-04-20 15:13:26 +01:00
|
|
|
T: 'static,
|
2026-04-21 20:30:41 +01:00
|
|
|
Store<F::Context>: gpui::Global,
|
2026-04-20 15:13:26 +01:00
|
|
|
{
|
2026-04-22 12:41:33 +01:00
|
|
|
let ent = cx.update_global::<Store<F::Context>, _>(|store, cx| {
|
|
|
|
|
let ent = store.entity_for(&query_fn, cx);
|
|
|
|
|
store.ensure_query_data(&query_fn, cx);
|
|
|
|
|
ent
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cx.observe(&ent.raw, move |_, ent, cx| {
|
|
|
|
|
let query = ent.read(cx);
|
|
|
|
|
if matches!(query.data, QueryData::Stale) {
|
|
|
|
|
cx.update_global::<Store<F::Context>, _>(|store, cx| {
|
|
|
|
|
store.ensure_query_data(&query_fn, cx);
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-04-20 15:13:26 +01:00
|
|
|
cx.notify();
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
2026-04-20 22:54:31 +01:00
|
|
|
|
2026-04-22 12:41:33 +01:00
|
|
|
let cloned_ent = ent.clone();
|
2026-04-21 20:30:41 +01:00
|
|
|
let query_context = cx.global::<Store<F::Context>>().query_context.clone();
|
|
|
|
|
cx.observe(&query_context, move |_, _, cx| {
|
|
|
|
|
cx.update_global::<Store<F::Context>, _>(|store, cx| {
|
2026-04-22 12:41:33 +01:00
|
|
|
store.invalidate_query(&cloned_ent, cx);
|
|
|
|
|
});
|
2026-04-21 20:30:41 +01:00
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
|
|
|
|
|
ent
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn use_lazy_query<F, T>(query_fn: F, cx: &mut gpui::Context<T>) -> Entity<F>
|
|
|
|
|
where
|
|
|
|
|
F: QueryFn,
|
|
|
|
|
T: 'static,
|
|
|
|
|
Store<F::Context>: gpui::Global,
|
|
|
|
|
{
|
|
|
|
|
let ent = cx.update_global::<Store<F::Context>, _>(|store, cx| store.entity_for(&query_fn, cx));
|
|
|
|
|
|
2026-04-22 12:41:33 +01:00
|
|
|
cx.observe(&ent.raw, move |_, ent, cx| {
|
|
|
|
|
let query = ent.read(cx);
|
|
|
|
|
if matches!(query.data, QueryData::Stale) {
|
|
|
|
|
cx.update_global::<Store<F::Context>, _>(|store, cx| {
|
|
|
|
|
store.ensure_query_data(&query_fn, cx);
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-04-21 20:30:41 +01:00
|
|
|
cx.notify();
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
2026-04-20 22:54:31 +01:00
|
|
|
|
2026-04-22 12:41:33 +01:00
|
|
|
let cloned_ent = ent.clone();
|
2026-04-21 20:30:41 +01:00
|
|
|
let query_context = cx.global::<Store<F::Context>>().query_context.clone();
|
2026-04-20 22:54:31 +01:00
|
|
|
cx.observe(&query_context, move |_, _, cx| {
|
2026-04-21 20:30:41 +01:00
|
|
|
cx.update_global::<Store<F::Context>, _>(|store, cx| {
|
2026-04-22 12:41:33 +01:00
|
|
|
store.invalidate_query(&cloned_ent, cx);
|
|
|
|
|
});
|
2026-04-20 22:54:31 +01:00
|
|
|
})
|
|
|
|
|
.detach();
|
2026-04-21 20:30:41 +01:00
|
|
|
|
|
|
|
|
ent
|
2026-04-20 15:13:26 +01:00
|
|
|
}
|
|
|
|
|
|
2026-04-22 12:41:33 +01:00
|
|
|
impl<F> Entity<F>
|
|
|
|
|
where
|
|
|
|
|
F: QueryFn,
|
|
|
|
|
Store<F::Context>: gpui::Global,
|
|
|
|
|
{
|
|
|
|
|
pub fn refetch(&self, cx: &mut gpui::Context<F::Context>) {
|
|
|
|
|
cx.update_global::<Store<F::Context>, _>(|store, cx| {
|
|
|
|
|
store.invalidate_query(self, cx);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 20:30:41 +01:00
|
|
|
pub fn read_query<'a, F, T>(
|
|
|
|
|
query: &Entity<F>,
|
2026-04-21 11:50:04 +01:00
|
|
|
cx: &'a gpui::Context<T>,
|
|
|
|
|
) -> QueryStatus<'a, F::Data, F::Error>
|
|
|
|
|
where
|
2026-04-21 20:30:41 +01:00
|
|
|
F: QueryFn,
|
2026-04-21 11:50:04 +01:00
|
|
|
T: 'static,
|
|
|
|
|
{
|
2026-04-21 20:30:41 +01:00
|
|
|
let state = query.raw.read(cx);
|
|
|
|
|
|
|
|
|
|
match &state.data {
|
2026-04-21 11:50:04 +01:00
|
|
|
QueryData::Loading | QueryData::Pending | QueryData::Stale => QueryStatus::Loading,
|
|
|
|
|
QueryData::Some(data) => QueryStatus::Loaded(data.downcast_ref::<F::Data>().unwrap()),
|
|
|
|
|
QueryData::Err(error) => QueryStatus::Err(error.downcast_ref::<F::Error>().unwrap()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 15:13:26 +01:00
|
|
|
// ================= Store ==================
|
|
|
|
|
|
2026-04-21 11:50:04 +01:00
|
|
|
pub(crate) trait Context: Clone {}
|
2026-04-20 22:54:31 +01:00
|
|
|
|
|
|
|
|
pub struct Store<C>
|
|
|
|
|
where
|
|
|
|
|
C: Context,
|
|
|
|
|
{
|
2026-04-21 20:30:41 +01:00
|
|
|
query_data: HashMap<String, gpui::Entity<Query>>,
|
2026-04-20 22:54:31 +01:00
|
|
|
query_context: gpui::Entity<C>,
|
2026-04-20 15:13:26 +01:00
|
|
|
}
|
|
|
|
|
|
2026-04-20 22:54:31 +01:00
|
|
|
impl<C> Store<C>
|
|
|
|
|
where
|
|
|
|
|
C: Context + 'static,
|
|
|
|
|
{
|
2026-04-21 11:50:04 +01:00
|
|
|
pub fn new(ctx: C, cx: &mut gpui::App) -> Self {
|
2026-04-20 15:13:26 +01:00
|
|
|
Self {
|
2026-04-20 22:54:31 +01:00
|
|
|
query_context: cx.new(|_| ctx),
|
2026-04-22 12:41:33 +01:00
|
|
|
query_data: std::collections::HashMap::new(),
|
2026-04-20 15:13:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 20:30:41 +01:00
|
|
|
fn entity_for<Q, T>(&mut self, query: &Q, cx: &mut gpui::Context<T>) -> Entity<Q>
|
2026-04-20 15:13:26 +01:00
|
|
|
where
|
2026-04-21 20:30:41 +01:00
|
|
|
Q: QueryFn<Context = C>,
|
2026-04-20 15:13:26 +01:00
|
|
|
T: 'static,
|
|
|
|
|
{
|
2026-04-21 20:30:41 +01:00
|
|
|
let raw = self
|
|
|
|
|
.query_data
|
2026-04-20 15:13:26 +01:00
|
|
|
.entry(query.key().into())
|
|
|
|
|
.or_insert_with(|| {
|
2026-04-21 20:30:41 +01:00
|
|
|
cx.new(|_| Query {
|
|
|
|
|
key: query.key(),
|
2026-04-20 15:13:26 +01:00
|
|
|
data: QueryData::Pending,
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-04-21 20:30:41 +01:00
|
|
|
.clone();
|
|
|
|
|
|
|
|
|
|
Entity {
|
|
|
|
|
raw,
|
|
|
|
|
_marker: PhantomData,
|
|
|
|
|
}
|
2026-04-20 15:13:26 +01:00
|
|
|
}
|
|
|
|
|
|
2026-04-21 20:30:41 +01:00
|
|
|
fn ensure_query_data<Q, T>(&mut self, query: &Q, cx: &mut gpui::Context<T>) -> Entity<Q>
|
2026-04-20 22:54:31 +01:00
|
|
|
where
|
|
|
|
|
T: 'static,
|
2026-04-21 20:30:41 +01:00
|
|
|
Q: QueryFn<Context = C>,
|
2026-04-20 22:54:31 +01:00
|
|
|
{
|
|
|
|
|
let entity = self.entity_for(query, cx);
|
|
|
|
|
|
2026-04-21 20:30:41 +01:00
|
|
|
let should_execute = entity.raw.read_with(cx, |state, _| {
|
2026-04-20 22:54:31 +01:00
|
|
|
matches!(state.data, QueryData::Pending | QueryData::Stale)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if should_execute {
|
|
|
|
|
self.execute_query(query, cx).detach();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entity
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 15:13:26 +01:00
|
|
|
fn execute_query<Q, T>(
|
|
|
|
|
&mut self,
|
2026-04-20 22:54:31 +01:00
|
|
|
query: &Q,
|
2026-04-20 15:13:26 +01:00
|
|
|
cx: &mut gpui::Context<T>,
|
|
|
|
|
) -> gpui::Task<anyhow::Result<()>>
|
|
|
|
|
where
|
2026-04-21 20:30:41 +01:00
|
|
|
Q: QueryFn<Context = C>,
|
2026-04-20 15:13:26 +01:00
|
|
|
T: 'static,
|
|
|
|
|
{
|
2026-04-20 22:54:31 +01:00
|
|
|
let entity = self.entity_for(query, cx);
|
|
|
|
|
|
2026-04-21 20:30:41 +01:00
|
|
|
entity.raw.update(cx, |state, cx| {
|
2026-04-20 15:13:26 +01:00
|
|
|
state.data = QueryData::Loading;
|
|
|
|
|
cx.notify();
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-21 11:50:04 +01:00
|
|
|
let q = query.clone();
|
|
|
|
|
let query_context = self.query_context.clone();
|
|
|
|
|
|
2026-04-20 15:13:26 +01:00
|
|
|
cx.spawn(async move |_, cx| {
|
2026-04-21 11:50:04 +01:00
|
|
|
let c = query_context.read_with(cx, |c, _| c.clone())?;
|
|
|
|
|
let result = q.run(&c).await;
|
2026-04-20 15:13:26 +01:00
|
|
|
|
2026-04-21 20:30:41 +01:00
|
|
|
entity.raw.update(cx, |state, cx| {
|
2026-04-20 15:13:26 +01:00
|
|
|
state.data = match result {
|
|
|
|
|
Ok(data) => QueryData::Some(Box::new(data)),
|
|
|
|
|
Err(err) => QueryData::Err(Box::new(err)),
|
|
|
|
|
};
|
|
|
|
|
cx.notify();
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
anyhow::Ok(())
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 12:41:33 +01:00
|
|
|
fn invalidate_query<E, F>(&self, entity: &Entity<F>, cx: &mut gpui::Context<E>)
|
2026-04-20 22:54:31 +01:00
|
|
|
where
|
2026-04-22 12:41:33 +01:00
|
|
|
E: 'static,
|
|
|
|
|
F: QueryFn<Context = C>,
|
2026-04-20 22:54:31 +01:00
|
|
|
{
|
2026-04-22 12:41:33 +01:00
|
|
|
let entity = entity.raw.read(cx);
|
|
|
|
|
if let Some(entity) = self.query_data.get(entity.key) {
|
2026-04-20 22:54:31 +01:00
|
|
|
entity.update(cx, |query, cx| {
|
|
|
|
|
if !matches!(query.data, QueryData::Loading) {
|
|
|
|
|
query.data = QueryData::Stale;
|
|
|
|
|
cx.notify()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-20 15:13:26 +01:00
|
|
|
}
|
2026-04-21 11:50:04 +01:00
|
|
|
|
|
|
|
|
impl<C> gpui::Global for Store<C> where C: Context + 'static {}
|