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

Creates a new Query Client.

source

pub fn fetch_query<K, V, Fu>( &self, key: impl Fn() -> K + 'static, fetcher: impl Fn(K) -> Fu + 'static, isomorphic: bool ) -> QueryResult<V, impl RefetchFn>
where K: Hash + Eq + Clone + 'static, V: Clone + 'static, Fu: Future<Output = V> + 'static,

Prefetch a query and store it in cache. Returns QueryResult. If the entry already exists it will still be refetched.

If you don’t need the result opt for prefetch_query()

source

pub fn prefetch_query<K, V, Fu>( &self, key: impl Fn() -> K + 'static, query: impl Fn(K) -> Fu + 'static, isomorphic: bool )
where K: Hash + Eq + Clone + 'static, V: Clone + '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: Hash + Eq + Clone + 'static, V: Clone,

Retrieve the current state for an existing query. If the query does not exist, None will be returned.

source

pub fn invalidate_query<K, V>(&self, key: impl Borrow<K>) -> bool
where K: Hash + Eq + Clone + 'static, V: Clone + '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:

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: Hash + Eq + Clone + 'static, V: Clone + 'static, Q: Borrow<K>,

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:

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) -> &Self
where K: Clone + 'static, V: Clone + 'static,

Invalidate all queries with a common <K, V> type.

Example:

use leptos::*;
use leptos_query::*;

let client = use_query_client();
client.invalidate_query_type::<String, Monkey>();
source

pub fn invalidate_all_queries(&self) -> &Self

Invalidates all queries in the cache.

Example:

use leptos::*;
use leptos_query::*;

let client = use_query_client();
client.invalidate_all_queries();
source

pub fn size(&self) -> Signal<usize>

Returns the current size of the cache.

Example:

use leptos::*;
use leptos_query::*;

let client = use_query_client();
let cache_size = client.size();
source

pub fn set_query_data<K, V>( &self, key: K, updater: impl FnOnce(Option<&V>) -> Option<V> + 'static ) -> &Self
where K: Clone + Eq + Hash + 'static, V: Clone + '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::*;
use leptos_query::*;

let client = use_query_client();
let new_monkey: Monkey = todo!();

// Overwrites existing cache data.
client.set_query_data::<u32, Monkey>(1, |_| Some(new_monkey));

// Only updates if query data exists.
client.set_query_data::<u32, Monkey>(1, |maybe_monkey| {
    let prev_monkey = maybe_monkey?;
    new_monkey
});

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.

§

impl<T> Instrument for T

§

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

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

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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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