Skip to content

Commit

Permalink
fix: Double loading invalidation (#25)
Browse files Browse the repository at this point in the history
* fix: double invalidate bug

Currently, if a query gets invalidated while its state is Loading(Some(value)), it loses the value. This can be annoying in cases where the query is invalidated multiple times within a short timespan, or for long-running queries.

We can fix this by nooping set_loading calls for queries that are already loading, which is what this change adds.

* reusing is_loading helper

* refactor: Cleaner fix

---------

Co-authored-by: marc2332 <mespinsanz@gmail.com>
  • Loading branch information
sinasab and marc2332 authored Mar 1, 2025
1 parent 8c4cde0 commit 77466cf
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/result.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::mem;

use crate::cached_result::CachedResult;

/// The result of a query.
#[derive(Clone, PartialEq, Debug)]
pub enum QueryResult<T, E> {
Expand All @@ -27,8 +25,8 @@ impl<T, E> QueryResult<T, E> {
}

pub fn set_loading(&mut self) {
let result = mem::replace(self, Self::Loading(None));
if let Self::Ok(v) = result {
let result = mem::replace(self, Self::Loading(None)).into();
if let Some(v) = result {
*self = Self::Loading(Some(v))
}
}
Expand All @@ -40,9 +38,9 @@ impl<T, E> Default for QueryResult<T, E> {
}
}

impl<T, E> From<CachedResult<T, E>> for Option<T> {
fn from(result: CachedResult<T, E>) -> Self {
match result.value {
impl<T, E> From<QueryResult<T, E>> for Option<T> {
fn from(result: QueryResult<T, E>) -> Self {
match result {
QueryResult::Ok(v) => Some(v),
QueryResult::Err(_) => None,
QueryResult::Loading(v) => v,
Expand Down

0 comments on commit 77466cf

Please sign in to comment.