some more bs

This commit is contained in:
2026-04-21 11:50:04 +01:00
parent 74a01bd9e6
commit 6c60013295
11 changed files with 177 additions and 58 deletions

View File

@@ -1,10 +1,9 @@
use crate::app;
use gpui::{AppContext, BorrowAppContext};
use std::any::Any;
pub trait QueryFn<C>: Clone + 'static
where
C: Context + 'static,
C: Context,
{
type Data: 'static;
type Error: 'static;
@@ -45,6 +44,27 @@ where
.detach();
}
pub fn read_query<'a, F, T, C>(
query_fn: F,
cx: &'a gpui::Context<T>,
) -> QueryStatus<'a, F::Data, F::Error>
where
C: Context + 'static,
F: QueryFn<C>,
T: 'static,
{
let store = cx.global::<Store<C>>();
let Some(ent) = store.query_data.get(query_fn.key()) else {
return QueryStatus::Loading;
};
let QueryState { data } = ent.read(cx);
match data {
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()),
}
}
// ================= Store ==================
pub(crate) struct QueryState {
@@ -61,7 +81,7 @@ pub(crate) enum QueryData {
pub(crate) type Entity = gpui::Entity<QueryState>;
pub(crate) trait Context {}
pub(crate) trait Context: Clone {}
pub struct Store<C>
where
@@ -75,7 +95,7 @@ impl<C> Store<C>
where
C: Context + 'static,
{
pub fn new<E>(ctx: C, cx: &mut gpui::Context<E>) -> Self {
pub fn new(ctx: C, cx: &mut gpui::App) -> Self {
Self {
query_data: std::collections::HashMap::new(),
query_context: cx.new(|_| ctx),
@@ -131,9 +151,12 @@ where
cx.notify();
});
let q = query.clone();
let query_context = self.query_context.clone();
cx.spawn(async move |_, cx| {
let query_context = self.query_context.read(cx);
let result = query.run(query_context).await;
let c = query_context.read_with(cx, |c, _| c.clone())?;
let result = q.run(&c).await;
entity.update(cx, |state, cx| {
state.data = match result {
@@ -180,3 +203,5 @@ where
}
}
}
impl<C> gpui::Global for Store<C> where C: Context + 'static {}