leptos_query/query_executor.rs
1use std::cell::Cell;
2
3/// Disable or enable query loading.
4///
5/// Useful for disabling query loads during App introspection, such as SSR Router integrations for Actix/Axum.
6///
7/// Example for `generate_route_list`
8/// ```
9/// use leptos::*;
10/// use leptos_query::*;
11/// use leptos_axum::*;
12///
13/// fn make_routes() {
14/// // Disable query loading.
15/// leptos_query::suppress_query_load(true);
16/// // Introspect App Routes.
17/// leptos_axum::generate_route_list(App);
18/// // Enable query loading.
19/// leptos_query::suppress_query_load(false);
20/// }
21///
22/// #[component]
23/// fn App() -> impl IntoView {
24/// ()
25/// }
26/// ```
27pub fn suppress_query_load(suppress: bool) {
28 SUPPRESS_QUERY_LOAD.with(|w| w.set(suppress));
29}
30
31/// Run a closure with query loading suppressed.
32///
33/// Useful for disabling query loads during App introspection, such as SSR Router integrations for Actix/Axum.
34///
35/// Example for `generate_route_list`
36/// ```
37/// use leptos::*;
38/// use leptos_query::*;
39/// use leptos_axum::*;
40///
41/// fn make_routes() {
42/// let routes = with_query_suppression(|| generate_route_list(App));
43/// }
44///
45/// #[component]
46/// fn App() -> impl IntoView {
47/// ()
48/// }
49/// ```
50pub fn with_query_suppression<T>(f: impl FnOnce() -> T) -> T {
51 SUPPRESS_QUERY_LOAD.with(|w| {
52 w.set(true);
53 let result = f();
54 w.set(false);
55 result
56 })
57}
58
59pub(crate) fn query_is_suppressed() -> bool {
60 SUPPRESS_QUERY_LOAD.get()
61}
62
63thread_local! {
64 static SUPPRESS_QUERY_LOAD: Cell<bool> = Cell::new(false);
65}