Struct leptos_query::QueryClient
source · pub struct QueryClient { /* private fields */ }
Expand description
The Cache Client to store query data. Exposes utility functions to manage queries.
Queries can be:
- Prefetched
- Query will start loading before you invoke use_query.
- Invalidated
- Query will refetch on next usage. Active queries are immediately refetched in the background.
- Introspected
- Let’s you see what the current value is of a query is.
- Manually updated
- Useful when you have updated a value and you want to manually set it in cache instead of waiting for query to refetch.
Implementations§
source§impl QueryClient
impl QueryClient
sourcepub fn new(owner: Owner, default_options: DefaultQueryOptions) -> Self
pub fn new(owner: Owner, default_options: DefaultQueryOptions) -> Self
Creates a new Query Client.
sourcepub async fn fetch_query<K, V, Fu>(
&self,
key: K,
fetcher: impl Fn(K) -> Fu + 'static
) -> QueryState<V>
pub async fn fetch_query<K, V, Fu>( &self, key: K, fetcher: impl Fn(K) -> Fu + 'static ) -> QueryState<V>
Fetch a query and store it in cache. Returns QueryResult. Result can be read outside of Transition.
If you don’t need the result opt for prefetch_query()
sourcepub async fn prefetch_query<K, V, Fu>(
&self,
key: K,
fetcher: impl Fn(K) -> Fu + 'static
)
pub async fn prefetch_query<K, V, Fu>( &self, key: K, fetcher: impl Fn(K) -> Fu + 'static )
Prefetch a query and store it in cache. If the entry already exists it will still be refetched.
If you need the result opt for fetch_query()
sourcepub fn get_query_state<K, V>(
&self,
key: impl Fn() -> K + 'static
) -> Signal<Option<QueryState<V>>>where
K: QueryKey + 'static,
V: QueryValue + 'static,
pub fn get_query_state<K, V>(
&self,
key: impl Fn() -> K + 'static
) -> Signal<Option<QueryState<V>>>where
K: QueryKey + 'static,
V: QueryValue + 'static,
Retrieve the current state for an existing query.
If the query does not exist, None
will be returned.
sourcepub fn peek_query_state<K, V>(&self, key: &K) -> Option<QueryState<V>>where
K: QueryKey + 'static,
V: QueryValue + 'static,
pub fn peek_query_state<K, V>(&self, key: &K) -> Option<QueryState<V>>where
K: QueryKey + 'static,
V: QueryValue + 'static,
Retrieve the current state for an existing query.
If the query does not exist, None
will be returned.
Useful for when you want to introspect the state of a query without subscribing to it.
sourcepub fn invalidate_query<K, V>(&self, key: impl Borrow<K>) -> boolwhere
K: QueryKey + 'static,
V: QueryValue + 'static,
pub fn invalidate_query<K, V>(&self, key: impl Borrow<K>) -> boolwhere
K: QueryKey + 'static,
V: QueryValue + 'static,
Attempts to invalidate an entry in the Query Cache. Matching query is marked as invalid, and will be refetched in background once it’s active.
Returns true if the entry was successfully invalidated.
Example:
use leptos_query::*;
use leptos_query::*;
fn invalidate() {
let client = use_query_client();
let invalidated = client.invalidate_query::<u32, u32>(0);
}
sourcepub fn invalidate_queries<K, V, Q>(
&self,
keys: impl IntoIterator<Item = Q>
) -> Option<Vec<Q>>
pub fn invalidate_queries<K, V, Q>( &self, keys: impl IntoIterator<Item = Q> ) -> Option<Vec<Q>>
Attempts to invalidate multiple entries in the Query Cache with a common <K, V> type. All matching queries are immediately marked as invalid and active queries are refetched in the background.
Returns the keys that were successfully invalidated.
Example:
use leptos_query::*;
fn invalidate() {
let client = use_query_client();
let keys: Vec<u32> = vec![0, 1];
let invalidated = client.invalidate_queries::<u32, u32, _>(keys);
}
sourcepub fn invalidate_query_type<K, V>(&self)where
K: QueryKey + 'static,
V: QueryValue + 'static,
pub fn invalidate_query_type<K, V>(&self)where
K: QueryKey + 'static,
V: QueryValue + 'static,
Invalidate all queries with a common <K, V> type.
Example:
use leptos_query::*;
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
struct MonkeyId(u32);
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
struct Monkey {
name: String
}
fn invalidate() {
let client = use_query_client();
let keys: Vec<u32> = vec![0, 1];
let invalidated = client.invalidate_query_type::<String, Monkey>();
}
sourcepub fn invalidate_all_queries(&self)
pub fn invalidate_all_queries(&self)
Invalidates all queries in the cache.
Example:
use leptos::*;
use leptos_query::*;
fn invalidate() {
let client = use_query_client();
let keys: Vec<u32> = vec![0, 1];
let invalidated = client.invalidate_all_queries();
}
sourcepub fn size(&self) -> Signal<usize>
pub fn size(&self) -> Signal<usize>
Returns the current size of the cache.
Example:
use leptos::*;
use leptos_query::*;
fn invalidate() {
let client = use_query_client();
let cache_size = client.size();
}
sourcepub fn update_query_data<K, V>(
&self,
key: K,
updater: impl FnOnce(Option<&V>) -> Option<V> + 'static
)where
K: QueryKey + 'static,
V: QueryValue + 'static,
pub fn update_query_data<K, V>(
&self,
key: K,
updater: impl FnOnce(Option<&V>) -> Option<V> + 'static
)where
K: QueryKey + 'static,
V: QueryValue + 'static,
A synchronous function that can be used to immediately set a query’s data.
If the query does not exist, it will be created.
If you need to fetch the data asynchronously, use fetch_query
or prefetch_query
.
If the updater function returns None
, the query data will not be updated.
If the updater function receives None
as input, you can return None
to bail out of the update and thus not create a new cache entry.
Example:
use leptos_query::*;
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
struct MonkeyId(u32);
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
struct Monkey {
name: String
}
fn invalidate() {
let client = use_query_client();
// Overwrite existing data.
client.update_query_data::<MonkeyId, Monkey>(MonkeyId(0), |_| Some(Monkey { name: "George".to_string() }));
// Don't overwrite George.
client.update_query_data::<MonkeyId, Monkey>(MonkeyId(0), |probably_george| {
if let Some(Monkey { name }) = probably_george {
if name == "George" {
return None;
}
}
Some(Monkey { name: "Luffy".to_string() })
});
}
sourcepub fn set_query_data<K, V>(&self, key: K, data: V)where
K: QueryKey + 'static,
V: QueryValue + 'static,
pub fn set_query_data<K, V>(&self, key: K, data: V)where
K: QueryKey + 'static,
V: QueryValue + 'static,
Update the query’s data. If the query does not exist, it will be created.
sourcepub fn update_query_data_mut<K, V>(
&self,
key: impl Borrow<K>,
updater: impl FnOnce(&mut V)
) -> boolwhere
K: QueryKey + 'static,
V: QueryValue + 'static,
pub fn update_query_data_mut<K, V>(
&self,
key: impl Borrow<K>,
updater: impl FnOnce(&mut V)
) -> boolwhere
K: QueryKey + 'static,
V: QueryValue + 'static,
Mutate the existing data if it exists. All listeners will be notified, regardless of whether the data was updated or not.
sourcepub fn cancel_query<K, V>(&self, key: K) -> boolwhere
K: QueryKey + 'static,
V: QueryValue + 'static,
pub fn cancel_query<K, V>(&self, key: K) -> boolwhere
K: QueryKey + 'static,
V: QueryValue + 'static,
Cancel any currently executing query. Returns whether the query was cancelled or not.
sourcepub fn register_cache_observer(&self, observer: impl CacheObserver + 'static)
pub fn register_cache_observer(&self, observer: impl CacheObserver + 'static)
Registers the cache observer.
sourcepub fn add_persister(&self, persister: impl QueryPersister + Clone + 'static)
pub fn add_persister(&self, persister: impl QueryPersister + Clone + 'static)
Adds a persister to the cache.
sourcepub fn remove_persister(&self) -> bool
pub fn remove_persister(&self) -> bool
Removes the persister from the cache.
Trait Implementations§
source§impl Clone for QueryClient
impl Clone for QueryClient
source§fn clone(&self) -> QueryClient
fn clone(&self) -> QueryClient
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more