Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "dioxus-query"
description = "Fully-typed, async, reusable cached state management for Dioxus 🧬"
version = "0.8.0"
version = "0.8.1"
edition = "2021"
license = "MIT"
authors = ["Marc Espín <[email protected]>"]
Expand All @@ -17,6 +17,11 @@ futures-util = "0.3.28"
warnings = "0.2.1"
tokio = { version = "^1", features = ["sync", "time"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasmtimer = "0.4.1"
web-time = "1.1.0"
tokio = { version = "^1", features = ["sync"] }

[dev-dependencies]
dioxus = { version = "0.6", features = ["desktop"] }
tokio = { version = "^1", features = ["time"] }
9 changes: 8 additions & 1 deletion src/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ use dioxus_lib::{
hooks::{use_memo, use_reactive},
signals::CopyValue,
};
#[cfg(not(target_family = "wasm"))]
use tokio::time;
#[cfg(not(target_family = "wasm"))]
use tokio::time::Instant;
#[cfg(target_family = "wasm")]
use wasmtimer::tokio as time;
#[cfg(target_family = "wasm")]
use web_time::Instant;

pub trait MutationCapability
where
Expand Down Expand Up @@ -182,7 +189,7 @@ impl<Q: MutationCapability> MutationsStorage<Q> {
if mutation_data.reactive_contexts.lock().unwrap().is_empty() {
*mutation_data.clean_task.borrow_mut() = spawn_forever(async move {
// Wait as long as the stale time is configured
tokio::time::sleep(mutation.clean_time).await;
time::sleep(mutation.clean_time).await;

// Finally clear the mutation
let mut storage = storage_clone.write();
Expand Down
31 changes: 22 additions & 9 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ use dioxus_lib::{
signals::CopyValue,
};
use futures_util::stream::{FuturesUnordered, StreamExt};
use tokio::{sync::Notify, time::Instant};
use tokio::sync::Notify;
#[cfg(not(target_family = "wasm"))]
use tokio::time;
#[cfg(not(target_family = "wasm"))]
use tokio::time::Instant;
#[cfg(target_family = "wasm")]
use wasmtimer::tokio as time;
#[cfg(target_family = "wasm")]
use web_time::Instant;

pub trait QueryCapability
where
Expand Down Expand Up @@ -222,7 +230,7 @@ impl<Q: QueryCapability> QueriesStorage<Q> {
let task = spawn_forever(async move {
loop {
// Wait as long as the stale time is configured
tokio::time::sleep(interval).await;
time::sleep(interval).await;

// Run the query
QueriesStorage::<Q>::run_queries(&[(&query_clone, &query_data_clone)]).await;
Expand Down Expand Up @@ -250,7 +258,7 @@ impl<Q: QueryCapability> QueriesStorage<Q> {
if query_data.reactive_contexts.lock().unwrap().is_empty() {
*query_data.clean_task.borrow_mut() = spawn_forever(async move {
// Wait as long as the stale time is configured
tokio::time::sleep(query.clean_time).await;
time::sleep(query.clean_time).await;

// Finally clear the query
let mut storage = storage_clone.write();
Expand Down Expand Up @@ -301,18 +309,18 @@ impl<Q: QueryCapability> QueriesStorage<Q> {
for reactive_context in query_data.reactive_contexts.lock().unwrap().iter() {
reactive_context.mark_dirty();
}
}

// Notify the suspense task if any
if let Some(suspense_task) = &*query_data.suspense_task.borrow() {
suspense_task.notifier.notify_waiters();
};
// Notify the suspense task if any
if let Some(suspense_task) = &*query_data.suspense_task.borrow() {
suspense_task.notifier.notify_waiters();
};
}

// Spawn clean up task if there no more reactive contexts
if query_data.reactive_contexts.lock().unwrap().is_empty() {
*query_data.clean_task.borrow_mut() = spawn_forever(async move {
// Wait as long as the stale time is configured
tokio::time::sleep(query.clean_time).await;
time::sleep(query.clean_time).await;

// Finally clear the query
let mut storage = storage.storage.write();
Expand Down Expand Up @@ -387,6 +395,11 @@ impl<Q: QueryCapability> QueriesStorage<Q> {
for reactive_context in query_data.reactive_contexts.lock().unwrap().iter() {
reactive_context.mark_dirty();
}

// Notify the suspense task if any
if let Some(suspense_task) = &*query_data.suspense_task.borrow() {
suspense_task.notifier.notify_waiters();
};
}));
}

Expand Down
Loading