pub struct QueryScopeLocal<K, V> { /* private fields */ }Expand description
A
non-threadsafe
wrapper for a query function. This can be used to add specific QueryOptions to only apply to one query scope.
These QueryOptions will be combined with the global QueryOptions set on the crate::QueryClient, with the local options taking precedence.
If you don’t need to set specific options, you can use functions with the crate::QueryClient directly.
Implementations§
Source§impl<Key, PageItem> QueryScopeLocal<PaginatedPageKey<Key>, Option<(Vec<PageItem>, bool)>>
impl<Key, PageItem> QueryScopeLocal<PaginatedPageKey<Key>, Option<(Vec<PageItem>, bool)>>
Sourcepub fn new_paginated_with_cursor<Cursor, Fut>(
getter: impl Fn(Key, usize, Option<Cursor>) -> Fut + 'static,
) -> QueryScopeLocal<PaginatedPageKey<Key>, Option<(Vec<PageItem>, bool)>>
pub fn new_paginated_with_cursor<Cursor, Fut>( getter: impl Fn(Key, usize, Option<Cursor>) -> Fut + 'static, ) -> QueryScopeLocal<PaginatedPageKey<Key>, Option<(Vec<PageItem>, bool)>>
Create a cursor-based paginated query scope.
Use this when your API uses continuation tokens/cursors rather than numeric offsets. Good for infinite scroll patterns or when your API doesn’t support offset-based pagination.
§Arguments
The getter receives:
query_key: Key- Your custom key, same across all pagesnb_items_requested: usize- Target number of items to return, will call again if not enough items returnedcursor: Option<Cursor>- Cursor from previous fetch,Noneon first page
The getter must return:
Vec<Item>- Items for this pageOption<Cursor>- Next cursor token,Nonewhen no more data
§Example
use leptos_fetch::{QueryScope, PaginatedPageKey};
// Create paginated scope with cursor-based API
let scope = QueryScope::new_paginated_with_cursor(
|_key: (), nb_items, cursor: Option<String>| async move {
// Call your API with cursor token
let (items, next_cursor) = fetch_from_api(cursor, nb_items).await;
(items, next_cursor)
}
);
// Use like any other scope - fetch pages with PaginatedPageKey
let (items, has_more) = client.fetch_query(scope, PaginatedPageKey {
key: (),
page_index: 0,
page_size: 20,
}).await.expect("Page exists");
// has_more: bool indicates if another page is available
if has_more {
let (next_items, _) = client.fetch_query(scope, PaginatedPageKey {
key: (),
page_index: 1,
page_size: 20,
}).await.expect("Next page exists");
}Source§impl<Key, PageItem> QueryScopeLocal<PaginatedPageKey<Key>, Option<(Vec<PageItem>, Option<u64>)>>
impl<Key, PageItem> QueryScopeLocal<PaginatedPageKey<Key>, Option<(Vec<PageItem>, Option<u64>)>>
Sourcepub fn new_paginated_with_offset<Fut>(
getter: impl Fn(Key, usize, u64) -> Fut + 'static,
) -> QueryScopeLocal<PaginatedPageKey<Key>, Option<(Vec<PageItem>, Option<u64>)>>
pub fn new_paginated_with_offset<Fut>( getter: impl Fn(Key, usize, u64) -> Fut + 'static, ) -> QueryScopeLocal<PaginatedPageKey<Key>, Option<(Vec<PageItem>, Option<u64>)>>
Create an offset-based paginated query scope.
Preferred when your API supports numeric offsets. Enables jumping to any page without loading intermediate pages and can return total item count for page calculations.
§Arguments
The getter receives:
query_key: Key- Your custom key, same across all pagesnb_items_requested: usize- Target number of items to returnoffset: u64- Starting position/offset for this fetch
The getter must return:
Vec<Item>- Items for this pageOption<u64>- Total item count if known (enables page count calculations)
§Example
use leptos_fetch::{QueryScope, PaginatedPageKey};
// Create paginated scope with offset-based API
let scope = QueryScope::new_paginated_with_offset(
|_key: (), nb_items, offset| async move {
// Call your API with offset and limit
let (items, total) = fetch_from_api(offset, nb_items).await;
(items, Some(total))
}
);
// Fetch page 0
let (items, total) = client.fetch_query(scope, PaginatedPageKey {
key: (),
page_index: 0,
page_size: 20,
}).await.expect("Page exists");
// Jump directly to page 10 (no need to load pages 1-9)
let (items, total) = client.fetch_query(scope, PaginatedPageKey {
key: (),
page_index: 10,
page_size: 20,
}).await.expect("Page exists");
// Calculate total pages from returned total count
if let Some(total_items) = total {
let total_pages = (total_items as f64 / 20.0).ceil() as u64;
}Source§impl<K, V> QueryScopeLocal<K, V>
impl<K, V> QueryScopeLocal<K, V>
Sourcepub fn new<M>(query_scope: impl QueryScopeLocalTrait<K, V, M> + 'static) -> Selfwhere
K: 'static,
V: 'static,
pub fn new<M>(query_scope: impl QueryScopeLocalTrait<K, V, M> + 'static) -> Selfwhere
K: 'static,
V: 'static,
Create a new
QueryScopeLocal
.
If the query fn does not have a key argument, K=()
Sourcepub fn with_options(self, options: QueryOptions) -> Self
pub fn with_options(self, options: QueryOptions) -> Self
Set specific QueryOptions to only apply to this query scope.
These QueryOptions will be combined with the global QueryOptions set on the crate::QueryClient, with the local options taking precedence.
Sourcepub fn with_invalidation_link<S, I>(
self,
invalidation_hierarchy_fn: impl Fn(&K) -> I + 'static,
) -> Self
pub fn with_invalidation_link<S, I>( self, invalidation_hierarchy_fn: impl Fn(&K) -> I + 'static, ) -> Self
Different query types are sometimes linked to the same source, e.g. you may want an invalidation of list_blogposts() to always automatically invalidate get_blogpost(id).
QueryScope::with_invalidation_link can be used to this effect, given a query key &K, you provide a Vec<String> that’s used as a hierarchy key (HK) for that query. When a query is invalidated, any query’s HK that’s prefixed by this HK will also be invalidated automatically. E.g. A query with HK ["users"] will also auto invalidate another query with ["users", "1"], but not the other way around. 2 queries with an identicial HK of ["users"] will auto invalidate each other.
use std::time::Duration;
use leptos_fetch::{QueryClient, QueryScope, QueryOptions};
use leptos::prelude::*;
#[derive(Debug, Clone)]
struct User;
fn list_users_query() -> QueryScope<(), Vec<User>> {
QueryScope::new(async || vec![])
.with_invalidation_link(
|_key| ["users"]
)
}
fn get_user_query() -> QueryScope<i32, User> {
QueryScope::new(async move |user_id: i32| User)
.with_invalidation_link(
|user_id| ["users".to_string(), user_id.to_string()]
)
}
let client = QueryClient::new();
// This invalidates only user "2", because ["users", "2"] is not a prefix of ["users"],
// list_users_query is NOT invalidated.
client.invalidate_query(get_user_query(), &2);
// This invalidates both queries, because ["users"] is a prefix of ["users", "$x"]
client.invalidate_query(list_users_query(), &());Sourcepub fn on_invalidation(self, on_invalidation_cb: impl Fn(&K) + 'static) -> Self
pub fn on_invalidation(self, on_invalidation_cb: impl Fn(&K) + 'static) -> Self
Run a callback when a query is invalidated. Will only run once when a query is first invalidated, and not on repeated invalidations before a refresh/reset.
Sourcepub fn on_gc(self, on_gc_cb: impl Fn(&K) + 'static) -> Self
pub fn on_gc(self, on_gc_cb: impl Fn(&K) + 'static) -> Self
Run a callback when a query is garbage collected.
Sourcepub fn with_title(self, title: impl Into<String>) -> Self
Available on crate features devtools or devtools-always only.
pub fn with_title(self, title: impl Into<String>) -> Self
devtools or devtools-always only.Set a custom query scope/type title that will show in devtools.
Trait Implementations§
Source§impl<K, V> Clone for QueryScopeLocal<K, V>
impl<K, V> Clone for QueryScopeLocal<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for QueryScopeLocal<K, V>
impl<K, V> !RefUnwindSafe for QueryScopeLocal<K, V>
impl<K, V> !Send for QueryScopeLocal<K, V>
impl<K, V> !Sync for QueryScopeLocal<K, V>
impl<K, V> Unpin for QueryScopeLocal<K, V>
impl<K, V> !UnwindSafe for QueryScopeLocal<K, V>
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.