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

source

pub fn new(owner: Owner, default_options: DefaultQueryOptions) -> Self

Creates a new Query Client.

source

pub async fn fetch_query<K, V, Fu>( &self, key: K, fetcher: impl Fn(K) -> Fu + 'static ) -> QueryState<V>
where K: QueryKey + 'static, V: QueryValue + 'static, Fu: Future<Output = V> + 'static,

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()

source

pub async fn prefetch_query<K, V, Fu>( &self, key: K, fetcher: impl Fn(K) -> Fu + 'static )
where K: QueryKey + 'static, V: QueryValue + 'static, Fu: Future<Output = V> + '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()

source

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.

source

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.

source

pub fn invalidate_query<K, V>(&self, key: impl Borrow<K>) -> bool
where 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);
}
source

pub fn invalidate_queries<K, V, Q>( &self, keys: impl IntoIterator<Item = Q> ) -> Option<Vec<Q>>
where K: QueryKey + 'static, V: QueryValue + 'static, Q: Borrow<K> + 'static,

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);
}
source

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>();
}
source

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();
}
source

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();
}
source

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() })
    });

}
source

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.

source

pub fn update_query_data_mut<K, V>( &self, key: impl Borrow<K>, updater: impl FnOnce(&mut V) ) -> bool
where 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.

source

pub fn cancel_query<K, V>(&self, key: K) -> bool
where K: QueryKey + 'static, V: QueryValue + 'static,

Cancel any currently executing query. Returns whether the query was cancelled or not.

source

pub fn register_cache_observer(&self, observer: impl CacheObserver + 'static)

Registers the cache observer.

source

pub fn add_persister(&self, persister: impl QueryPersister + Clone + 'static)

Adds a persister to the cache.

source

pub fn remove_persister(&self) -> bool

Removes the persister from the cache.

source

pub fn clear(&self)

Clears the cache. All queries will be removed.

Trait Implementations§

source§

impl Clone for QueryClient

source§

fn clone(&self) -> QueryClient

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more