Skip to content

Commit 77466cf

Browse files
sinasabmarc2332
andauthored
fix: Double loading invalidation (#25)
* 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 <[email protected]>
1 parent 8c4cde0 commit 77466cf

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/result.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::mem;
22

3-
use crate::cached_result::CachedResult;
4-
53
/// The result of a query.
64
#[derive(Clone, PartialEq, Debug)]
75
pub enum QueryResult<T, E> {
@@ -27,8 +25,8 @@ impl<T, E> QueryResult<T, E> {
2725
}
2826

2927
pub fn set_loading(&mut self) {
30-
let result = mem::replace(self, Self::Loading(None));
31-
if let Self::Ok(v) = result {
28+
let result = mem::replace(self, Self::Loading(None)).into();
29+
if let Some(v) = result {
3230
*self = Self::Loading(Some(v))
3331
}
3432
}
@@ -40,9 +38,9 @@ impl<T, E> Default for QueryResult<T, E> {
4038
}
4139
}
4240

43-
impl<T, E> From<CachedResult<T, E>> for Option<T> {
44-
fn from(result: CachedResult<T, E>) -> Self {
45-
match result.value {
41+
impl<T, E> From<QueryResult<T, E>> for Option<T> {
42+
fn from(result: QueryResult<T, E>) -> Self {
43+
match result {
4644
QueryResult::Ok(v) => Some(v),
4745
QueryResult::Err(_) => None,
4846
QueryResult::Loading(v) => v,

0 commit comments

Comments
 (0)