Observers
Observers bridge gpui-query’s resources to GPUI’s reactivity. An observer attaches to a resource entity and calls cx.notify() to trigger a re-render, but only when the resource’s status or data changes. This is the primitive that keeps use_query, use_infinite_query, and use_mutation from re-rendering your view on every internal state mutation.
The Observer type
Section titled “The Observer type”There is a single generic implementation, Observer<R>, parameterized over the resource kind. Three type aliases name the concrete observers you will use:
use gpui_query::client::{ QueryObserver, InfiniteQueryObserver, MutationObserver,};
type QueryObserver<T, E> = Observer<QueryResource<T, E>>;type InfiniteQueryObserver<T, E> = Observer<InfiniteQueryResource<T, E>>;type MutationObserver<V, T, E> = Observer<MutationResource<V, T, E>>;Each resource exposes its status via an inherent status() method; the public ObservableResource trait surfaces it generically so Observer<R> can deduplicate notifications for any resource kind.
Status deduplication
Section titled “Status deduplication”Without status deduplication, a raw cx.observe would call cx.notify() on every entity mutation, including intermediate ones like increment_retry() that leave the status at Loading. A query retrying three times would re-render the view two or three extra times for nothing.
Observer tracks the last status and data epoch it saw and notifies only when either changes:
// (sketch of the dedup logic inside Observer::observe)let last_status: Cell<Option<R::Status>> = Cell::new(None);let last_epoch: Cell<Option<u64>> = Cell::new(None);
cx.observe(&upgraded, move |_, entity, cx| { let resource = entity.read(cx); let current_status = resource.observable_status(); let current_epoch = resource.data_epoch(); if notify_on_change { let status_changed = last_status.get() != Some(current_status); let data_changed = last_epoch.get() != Some(current_epoch); if status_changed || data_changed { last_status.set(Some(current_status)); last_epoch.set(Some(current_epoch)); cx.notify(); // status or data actually changed } } else { cx.notify(); // unconditional mode }});The result: increment_retry() / prepare_retry() calls (status stays Loading) no longer trigger re-renders. A same-status data write, such as set_query_data refreshing Success data, still re-renders: queries and infinite queries track a live data epoch, while MutationResource keeps the constant default and stays status-only. Your view re-renders on the transitions that matter: Idle → LoadingEmpty → Success, Success → LoadingWithData → Success, Loading → Failure, and so on.
Creating and observing
Section titled “Creating and observing”Build an observer from an entity, then call observe(cx) to start watching. observe returns Option<Subscription> (None if the entity was already dropped). Store the returned subscription; dropping it ends the observation.
use gpui_query::client::QueryObserver;use gpui_query::QueryResource;
#[derive(Clone)]struct T;
#[derive(Clone, Debug)]struct E;
struct MyView { users: gpui::Entity<QueryResource<T, E>>, _subscription: gpui::Subscription,}
impl MyView { fn new(users: gpui::Entity<QueryResource<T, E>>, cx: &mut gpui::Context<Self>) -> Self { let observer = QueryObserver::<T, E>::new(&users); let subscription = observer.observe(cx) .expect("entity was just created; cannot be dropped"); Self { users, _subscription: subscription } }}ObserverConfig
Section titled “ObserverConfig”ObserverConfig::notify_on_status_change_only controls the dedup behavior. It defaults to true. Set it to false to get unconditional notifications on every entity mutation:
use gpui_query::client::{Observer, ObserverConfig};use gpui_query::QueryResource;
#[derive(Clone)]struct T;
#[derive(Clone, Debug)]struct E;
fn example(entity: gpui::Entity<QueryResource<T, E>>, cx: &mut gpui::Context<()>) { let sub = Observer::new(&entity) .with_config(ObserverConfig { notify_on_status_change_only: false }) .observe(cx);}Use unconditional mode only when you need to react to non-status fields (a custom retry counter you display inline, for example). It is strictly more re-renders.
Option<Subscription>
Section titled “Option<Subscription>”observe() returns Option<Subscription> rather than panicking. In debug builds the hooks panic! if this is None right after creating an entity (it would indicate a GPUI internal regression); in release builds they fall back to a no-op subscription so a misbehaving observer cannot crash the app. When you construct an observer yourself, decide explicitly: .expect(...) if a None is impossible in your context, or match / let else to handle it gracefully.
How the hooks use it
Section titled “How the hooks use it”| Hook | Observer | Status type |
|---|---|---|
use_query |
QueryObserver<T, E> |
QueryStatus |
use_infinite_query |
InfiniteQueryObserver<T, E> |
QueryStatus |
use_mutation |
MutationObserver<V, T, E> |
MutationStatus |
Each hook creates its observer, calls observe(cx), and returns the subscription as the second element of its tuple. The status-dedup is what makes a retrying mutation or a refetching query cheap to render: retry churn never reaches your view.
ObservableResource
Section titled “ObservableResource”ObservableResource is the public trait that lets Observer<R> read a status generically. It is re-exported at gpui_query::client::ObservableResource and the crate root. It is why the three resource kinds share one observer implementation: each resource exposes status() with its own associated Status type, and Observer deduplicates on that. Implementing it requires observable_status(); data_epoch() defaults to 0, and a custom resource should override it when same-status data writes should still re-render. For application code, the three built-in observers cover every resource the hooks produce.
Next steps
Section titled “Next steps”- Queries / Infinite queries / Mutations: the hooks that build observers for you.
- DevTools: diagnostics read the same live entities observers watch.
- Error handling: why
Cancelledrarely reaches your UI (it is a status, deduplicated like any other).