diff --git a/Cargo.toml b/Cargo.toml index 893bbf0..ace363f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 "] @@ -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"] } diff --git a/src/mutation.rs b/src/mutation.rs index 66ccf4d..580cb09 100644 --- a/src/mutation.rs +++ b/src/mutation.rs @@ -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 @@ -182,7 +189,7 @@ impl MutationsStorage { 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(); diff --git a/src/query.rs b/src/query.rs index 824271e..70b44a2 100644 --- a/src/query.rs +++ b/src/query.rs @@ -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 @@ -222,7 +230,7 @@ impl QueriesStorage { 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::::run_queries(&[(&query_clone, &query_data_clone)]).await; @@ -250,7 +258,7 @@ impl QueriesStorage { 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(); @@ -301,18 +309,18 @@ impl QueriesStorage { 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(); @@ -387,6 +395,11 @@ impl QueriesStorage { 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(); + }; })); }