#[cfg(feature = "query")]
use std::cell::Cell;
use std::{collections::HashMap, rc::Rc};
#[cfg(feature = "query")]
use gpui::AnyWindowHandle;
use gpui::{App, Global, SharedString, Window};
#[cfg(feature = "query")]
use super::history::CURRENT_WINDOW;
use super::{location, with_window, Location, Pattern, RouteMatch, Router};
pub(super) fn run_loader_once(route: &RouteMatch, loader: &Loader, cx: &mut App) {
let href = route.location.href();
let last = cx
.default_global::<Loaders>()
.last_loaded
.insert(route.pattern.clone(), href.clone());
if last.as_ref() == Some(&href) {
return;
}
#[cfg(feature = "query")]
let fetching_before = fetching_keys(cx);
loader(route, cx);
#[cfg(feature = "query")]
{
let started: Vec<_> = fetching_keys(cx)
.into_iter()
.filter(|key| !fetching_before.contains(key))
.collect();
let window = CURRENT_WINDOW.with(Cell::get);
let load = cx
.default_global::<Loaders>()
.started
.entry(window)
.or_insert_with(|| WindowLoad::new(href.clone()));
if !started.is_empty() && load.since.is_none() {
load.since = Some(std::time::Instant::now());
}
load.keys.extend(started);
}
}
#[cfg(feature = "query")]
pub(super) fn fetching_keys(cx: &App) -> Vec<crate::query::QueryKey> {
crate::query::queries(cx)
.into_iter()
.filter(|query| query.is_fetching)
.map(|query| query.key)
.collect()
}
#[cfg(feature = "query")]
pub(super) fn cancel_stale_loads(location: &Location, cx: &mut App) {
let href = location.href();
let window = CURRENT_WINDOW.with(Cell::get);
let previous = {
let loaders = cx.default_global::<Loaders>();
match loaders.started.get_mut(&window) {
Some(load) if load.shown == href => return,
Some(load) => std::mem::replace(load, WindowLoad::new(href)).keys,
None => {
loaders.started.insert(window, WindowLoad::new(href));
return;
}
}
};
let still_fetching = fetching_keys(cx);
for key in previous {
if still_fetching.contains(&key) {
crate::query::cancel_queries(cx, &key);
}
}
}
pub type Loader = Rc<dyn Fn(&RouteMatch, &mut App)>;
#[derive(Default)]
pub(super) struct Loaders {
registered: Vec<(Pattern, Loader)>,
last_loaded: HashMap<SharedString, SharedString>,
#[cfg(feature = "query")]
started: HashMap<Option<AnyWindowHandle>, WindowLoad>,
}
impl Global for Loaders {}
#[cfg(feature = "query")]
struct WindowLoad {
shown: SharedString,
keys: Vec<crate::query::QueryKey>,
since: Option<std::time::Instant>,
}
#[cfg(feature = "query")]
impl WindowLoad {
fn new(shown: SharedString) -> Self {
Self {
shown,
keys: Vec::new(),
since: None,
}
}
}
#[cfg(feature = "query")]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct LoadState {
pub is_loading: bool,
pub since: Option<std::time::Instant>,
}
#[cfg(feature = "query")]
#[must_use]
pub fn load_state(cx: &App) -> LoadState {
let window = CURRENT_WINDOW
.with(Cell::get)
.or_else(|| cx.active_window());
load_state_in(window, cx)
}
#[cfg(feature = "query")]
fn load_state_in(window: Option<AnyWindowHandle>, cx: &App) -> LoadState {
let Some(load) = cx
.try_global::<Loaders>()
.and_then(|loaders| loaders.started.get(&window))
else {
return LoadState::default();
};
let fetching = fetching_keys(cx);
LoadState {
is_loading: load.keys.iter().any(|key| fetching.contains(key)),
since: load.since,
}
}
#[cfg(feature = "query")]
#[derive(Default)]
struct Pending {
shown_at: Option<std::time::Instant>,
timer: Option<gpui::Task<()>>,
}
#[cfg(feature = "query")]
#[track_caller]
pub fn use_pending(
cx: &mut crate::Cx,
delay: std::time::Duration,
min: std::time::Duration,
) -> bool {
use std::time::Instant;
let state = cx.window.use_state(cx.app, |_, _| Pending::default());
let load = load_state_in(Some(cx.window.window_handle()), cx.app);
let now = Instant::now();
let shown_at = state.read(cx.app).shown_at;
let (show, wake) = match (load.is_loading, load.since, shown_at) {
(true, _, Some(_)) => (true, None),
(true, Some(since), None) if now >= since + delay => (true, None),
(true, Some(since), None) => (false, Some(since + delay)),
(false, _, Some(shown)) if now < shown + min => (true, Some(shown + min)),
_ => (false, None),
};
let window = cx.window.window_handle();
state.update(cx.app, |state, cx| {
state.shown_at = match (show, state.shown_at) {
(true, None) => Some(now),
(true, shown) => shown,
(false, _) => None,
};
state.timer = wake.map(|at| {
cx.spawn(async move |_, cx| {
cx.background_executor()
.timer(at.saturating_duration_since(Instant::now()))
.await;
window.update(cx, |_, window, _| window.refresh()).ok();
})
});
});
show
}
#[derive(Default)]
struct VisiblePreloads {
location: SharedString,
done: std::collections::HashSet<SharedString>,
}
impl Global for VisiblePreloads {}
pub(super) fn preload_once_visible(target: SharedString, window: &Window, cx: &mut App) {
let shown = with_window(window.window_handle(), || location(cx).href());
let seen = cx.default_global::<VisiblePreloads>();
if seen.location != shown {
seen.location = shown;
seen.done.clear();
}
if seen.done.insert(target.clone()) {
cx.defer(move |cx| preload(&target, cx));
}
}
pub fn preload(path: &str, cx: &mut App) {
let location = Location::parse(path);
let matching: Vec<(RouteMatch, Loader)> = cx
.try_global::<Loaders>()
.map(|loaders| {
loaders
.registered
.iter()
.filter_map(|(pattern, loader)| {
let (params, _) = pattern.matches(location.path())?;
Some((
RouteMatch {
pattern: pattern.source.clone(),
params,
location: location.clone(),
},
loader.clone(),
))
})
.collect()
})
.unwrap_or_default();
for (route, loader) in matching {
loader(&route, cx);
}
}
impl Router {
pub(super) fn register_loaders(&self, cx: &mut App) {
let loaders = cx.default_global::<Loaders>();
for route in &self.routes {
let Some(loader) = &route.loader else {
continue;
};
match loaders
.registered
.iter_mut()
.find(|(pattern, _)| pattern.source == route.pattern.source)
{
Some(registered) => registered.1 = loader.clone(),
None => loaders
.registered
.push((route.pattern.clone(), loader.clone())),
}
}
}
}