Skip to content

Commit d29e211

Browse files
committed
2 parents 356319c + f9d2cbf commit d29e211

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/cached_result.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ impl<T, E> CachedResult<T, E> {
3131
}
3232

3333
/// Check if this result is stale yet
34-
pub fn is_fresh(&self, stale_time: Duration) -> bool {
34+
pub fn is_stale(&self, stale_time: Duration) -> bool {
3535
if let Some(instant) = self.instant {
36-
instant.elapsed() < stale_time
36+
instant.elapsed() > stale_time
3737
} else {
38-
false
38+
true
3939
}
4040
}
4141

@@ -48,6 +48,10 @@ impl<T, E> CachedResult<T, E> {
4848
pub(crate) fn set_to_loading(&mut self) {
4949
self.state.set_loading();
5050
self.instant = Some(Instant::now());
51+
}
52+
53+
/// Set this result as loaded
54+
pub(crate) fn set_to_loaded(&mut self) {
5155
self.has_been_loaded = true;
5256
}
5357
}

src/use_query.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ where
6767
client: UseQueryClient<T, E, K>,
6868
registry_entry: RegistryEntry<K>,
6969
scope_id: ScopeId,
70+
stale_time: Duration,
7071
}
7172

7273
impl<T, E, K: Eq + Hash> Drop for UseQueryCleaner<T, E, K> {
@@ -84,8 +85,10 @@ impl<T, E, K: Eq + Hash> Drop for UseQueryCleaner<T, E, K> {
8485
// Remove this listener
8586
query_listeners.listeners.remove(&self.scope_id);
8687

88+
let is_stale = query_listeners.value.borrow().is_stale(self.stale_time);
89+
8790
// Clear the queries registry of this listener if it was the last one
88-
if query_listeners.listeners.is_empty() {
91+
if query_listeners.listeners.is_empty() && is_stale {
8992
queries_registry.remove(&self.registry_entry);
9093
}
9194
});
@@ -141,7 +144,7 @@ pub fn use_query<T, E, K, const N: usize>(
141144
query: impl FnOnce() -> Query<T, E, K>,
142145
) -> UseQuery<T, E, K>
143146
where
144-
T: 'static + PartialEq,
147+
T: 'static,
145148
E: 'static,
146149
K: 'static + Eq + Hash + Clone,
147150
{
@@ -177,7 +180,7 @@ where
177180

178181
// Asynchronously initialize the query value
179182
spawn({
180-
to_owned![registry_entry];
183+
to_owned![registry_entry, stale_time];
181184
async move {
182185
client.run_new_query(&registry_entry, stale_time).await;
183186
}
@@ -191,6 +194,7 @@ where
191194
client,
192195
registry_entry,
193196
scope_id: current_scope_id().unwrap(),
197+
stale_time,
194198
}),
195199
}
196200
})
@@ -249,7 +253,7 @@ pub fn use_get_query<T, E, K, Q, F, const N: usize>(
249253
query_fn: Q,
250254
) -> UseQuery<T, E, K>
251255
where
252-
T: 'static + PartialEq,
256+
T: 'static,
253257
E: 'static,
254258
K: 'static + Eq + Hash + Clone,
255259
Q: 'static + Fn(Vec<K>) -> F,

src/use_query_client.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ where
103103
listeners,
104104
} = self.get_entry(entry);
105105

106-
let is_fresh = value.borrow().is_fresh(stale_time);
106+
let is_stale = value.borrow().is_stale(stale_time);
107107
let is_loading = value.borrow().is_loading();
108-
let has_been_queried = value.borrow().has_been_loaded();
108+
let has_been_loaded = value.borrow().has_been_loaded();
109109

110-
if (!is_fresh && !is_loading) || !has_been_queried {
110+
if (is_stale && !is_loading) || !has_been_loaded {
111111
// If the query still has its initial state because it hasn't been loaded yet
112112
// we don't need to mark the value as loading, it would be an unnecesssary notification.
113-
if has_been_queried {
113+
if has_been_loaded {
114114
value.borrow_mut().set_to_loading();
115115
for listener in listeners {
116116
(self.scheduler.peek())(listener);
@@ -128,6 +128,8 @@ where
128128
for listener in listeners {
129129
(self.scheduler.peek())(listener);
130130
}
131+
132+
value.borrow_mut().set_to_loaded();
131133
}
132134
}
133135

0 commit comments

Comments
 (0)