Skip to content

Commit

Permalink
chore: Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Mar 2, 2025
1 parent dd0a408 commit e6c75fb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
50 changes: 16 additions & 34 deletions examples/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,27 @@ enum QueryValue {
}

async fn fetch_user(keys: Vec<QueryKey>) -> QueryResult<QueryValue, QueryError> {
if let Some(QueryKey::User(id)) = keys.first() {
println!("Fetching name of user {id}");
sleep(Duration::from_millis(650)).await;
match id {
0 => Ok(QueryValue::UserName("Marc".to_string())),
_ => Err(QueryError::UserNotFound(*id)),
}
} else {
Err(QueryError::Unknown)
let Some(QueryKey::User(id)) = keys.first() else {
return Err(QueryError::Unknown);
};
println!("Fetching name of user {id}");
sleep(Duration::from_millis(650)).await;
match id {
0 => Ok(QueryValue::UserName("Marc".to_string())),
_ => Err(QueryError::UserNotFound(*id)),
}
}

async fn fetch_user_age(keys: Vec<QueryKey>) -> QueryResult<QueryValue, QueryError> {
if let Some(QueryKey::User(id)) = keys.first() {
println!("Fetching age of user {id}");
sleep(Duration::from_millis(1000)).await;
match id {
0 => Ok(QueryValue::UserAge(0)),
_ => Err(QueryError::UserNotFound(*id)),
}
} else {
Err(QueryError::Unknown)
}
}

#[derive(Debug)]
enum MutationError {}

#[derive(PartialEq, Debug)]
enum MutationValue {
UserUpdated(usize),
}

async fn update_user((id, _name): (usize, String)) -> MutationResult<MutationValue, MutationError> {
println!("Mutating user");
let Some(QueryKey::User(id)) = keys.first() else {
return Err(QueryError::Unknown);
};
println!("Fetching age of user {id}");
sleep(Duration::from_millis(1000)).await;
Ok(MutationValue::UserUpdated(id))
match id {
0 => Ok(QueryValue::UserAge(0)),
_ => Err(QueryError::UserNotFound(*id)),
}
}

#[allow(non_snake_case)]
Expand All @@ -86,11 +70,9 @@ fn User(id: usize) -> Element {
}

fn app() -> Element {
let mutate = use_mutation(update_user);
let client = use_init_query_client::<QueryValue, QueryError, QueryKey>();

let refresh = move |_| async move {
mutate.mutate_async((0, "Not Marc".to_string())).await;
client.invalidate_queries(&[QueryKey::User(0)]);
};

Expand Down
4 changes: 2 additions & 2 deletions src/cached_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct CachedResult<T, E> {
}

impl<T, E> CachedResult<T, E> {
pub fn new(value: QueryState<T, E>) -> Self {
pub(crate) fn new(value: QueryState<T, E>) -> Self {
Self {
value,
..Default::default()
Expand All @@ -25,7 +25,7 @@ impl<T, E> CachedResult<T, E> {
}

/// Set this result's value
pub fn set_value(&mut self, new_value: QueryState<T, E>) {
pub(crate) fn set_value(&mut self, new_value: QueryState<T, E>) {
self.value = new_value;
self.instant = Some(Instant::now());
}
Expand Down
2 changes: 1 addition & 1 deletion src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<T, E> QueryState<T, E> {
matches!(self, QueryState::Loading(..))
}

pub fn set_loading(&mut self) {
pub(crate) fn set_loading(&mut self) {
let result = mem::replace(self, Self::Loading(None)).into();
if let Some(v) = result {
*self = Self::Loading(Some(v))
Expand Down

0 comments on commit e6c75fb

Please sign in to comment.