Skip to content

Commit

Permalink
Fix spawn local outside localset (#24)
Browse files Browse the repository at this point in the history
* fix resource.refetch when suppressed

* ensure query not suppressed in query execute

* cfg flags for persister

* add function with_query_supression
  • Loading branch information
nicoburniske committed Feb 20, 2024
1 parent 63e438d commit 08dbd81
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
4 changes: 3 additions & 1 deletion example/start-axum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ async fn main() {
use axum::{routing::post, Router};
use leptos::*;
use leptos_axum::{generate_route_list, LeptosRoutes};
use leptos_query::with_query_supression;
use start_axum::app::*;
use start_axum::fileserv::file_and_error_handler;
use tokio::net::TcpListener;
Expand All @@ -18,7 +19,8 @@ async fn main() {
let conf = get_configuration(None).await.unwrap();
let leptos_options = conf.leptos_options;
let addr = leptos_options.site_addr;
let routes = generate_route_list(App);

let routes = with_query_supression(|| generate_route_list(App));

// build our application with a route
let app = Router::new()
Expand Down
5 changes: 4 additions & 1 deletion query/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use leptos::*;
use crate::{
garbage_collector::GarbageCollector,
query_cache::CacheNotification,
query_is_supressed,
query_observer::{ObserverKey, QueryObserver},
use_query_client,
util::time_until_stale,
Expand Down Expand Up @@ -224,7 +225,9 @@ where
let fetcher = observers.values().find_map(|f| f.get_fetcher());

if let Some(fetcher) = fetcher {
spawn_local(execute_query(self.clone(), move |k| fetcher(k)));
if !query_is_supressed() {
spawn_local(execute_query(self.clone(), move |k| fetcher(k)));
}
}
}

Expand Down
1 change: 1 addition & 0 deletions query/src/query_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ impl QueryCache {
}
// Though persister receives removal events, there may be queries in persister that are not yet in cache.
// So we should clear them all.
#[cfg(any(feature = "hydrate", feature = "csr"))]
if let Some(persister) = self.persister.borrow().clone() {
spawn_local(async move {
persister.clear().await;
Expand Down
29 changes: 27 additions & 2 deletions query/src/query_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,37 @@ use std::cell::Cell;
/// fn App() -> impl IntoView {
/// ()
/// }
/// ```
pub fn suppress_query_load(suppress: bool) {
SUPPRESS_QUERY_LOAD.with(|w| w.set(suppress));
}

/// Run a closure with query loading suppressed.
///
/// Useful for disabling query loads during App introspection, such as SSR Router integrations for Actix/Axum.
///
/// Example for `generate_route_list`
/// ```
/// use leptos::*;
/// use leptos_query::*;
/// use leptos_axum::*;
///
/// fn make_routes() {
/// let routes = with_query_supression(|| generate_route_list(App));
/// }
///
/// #[component]
/// fn App() -> impl IntoView {
/// ()
/// }
/// ```
pub fn suppress_query_load(suppress: bool) {
SUPPRESS_QUERY_LOAD.with(|w| w.set(suppress));
pub fn with_query_supression<T>(f: impl FnOnce() -> T) -> T {
SUPPRESS_QUERY_LOAD.with(|w| {
w.set(true);
let result = f();
w.set(false);
result
})
}

pub(crate) fn query_is_supressed() -> bool {
Expand Down
8 changes: 6 additions & 2 deletions query/src/query_persister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,27 @@ where
{
fn process_cache_event(&self, event: CacheEvent) {
match event {
#[cfg(any(feature = "hydrate", feature = "csr"))]
CacheEvent::Created(query) => {
if let Ok(value) = query.state.try_into() {
if let Ok(value) = TryInto::<PersistQueryData>::try_into(query.state) {
let key = query.key.0;
let persister = self.clone();
leptos::spawn_local(async move {
persister.persist(&key, value).await;
})
}
}
#[cfg(any(feature = "hydrate", feature = "csr"))]
CacheEvent::Updated(query) => {
if let Ok(value) = query.state.try_into() {
if let Ok(value) = TryInto::<PersistQueryData>::try_into(query.state) {
let key = query.key.0;
let persister = self.clone();
leptos::spawn_local(async move {
persister.persist(&key, value).await;
})
}
}
#[cfg(any(feature = "hydrate", feature = "csr"))]
CacheEvent::Removed(key) => {
let persister = self.clone();
leptos::spawn_local(async move {
Expand Down Expand Up @@ -115,6 +118,7 @@ pub mod local_storage_persister {

#[cfg(any(feature = "hydrate", feature = "csr"))]
thread_local! {
#[cfg(any(feature = "hydrate", feature = "csr"))]
pub(crate) static LOCAL_STORAGE: Option<web_sys::Storage> = leptos::window().local_storage().ok().flatten()
}

Expand Down
9 changes: 7 additions & 2 deletions query/src/use_query.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::query::Query;
use crate::query_observer::{ListenerKey, QueryObserver};
use crate::query_result::QueryResult;
use crate::{use_query_client, QueryOptions, QueryState, RefetchFn, ResourceOption};
use crate::{
query_is_supressed, use_query_client, QueryOptions, QueryState, RefetchFn, ResourceOption,
};
use leptos::*;
use std::cell::Cell;
use std::future::Future;
Expand Down Expand Up @@ -111,7 +113,10 @@ where
// Ensure latest data in resource.
create_isomorphic_effect(move |_| {
query_state.track();
resource.refetch();
// If query is supressed, we have to make sure we don't refetch to avoid calling spawn_local.
if !query_is_supressed() {
resource.refetch();
}
});

let data = Signal::derive({
Expand Down

0 comments on commit 08dbd81

Please sign in to comment.