Skip to content

Commit 8c6b951

Browse files
authored
fix: WASM support and suspense fix (#41)
* fix: Use `wasmtimer` in wasm * use web-time for instants * notify waiters of suspense properly * fix imports * remove tokio::time calls * remove time::Instant call * bump to 0.8.1
1 parent 94a7527 commit 8c6b951

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "dioxus-query"
33
description = "Fully-typed, async, reusable cached state management for Dioxus 🧬"
4-
version = "0.8.0"
4+
version = "0.8.1"
55
edition = "2021"
66
license = "MIT"
77
authors = ["Marc Espín <[email protected]>"]
@@ -17,6 +17,11 @@ futures-util = "0.3.28"
1717
warnings = "0.2.1"
1818
tokio = { version = "^1", features = ["sync", "time"] }
1919

20+
[target.'cfg(target_arch = "wasm32")'.dependencies]
21+
wasmtimer = "0.4.1"
22+
web-time = "1.1.0"
23+
tokio = { version = "^1", features = ["sync"] }
24+
2025
[dev-dependencies]
2126
dioxus = { version = "0.6", features = ["desktop"] }
2227
tokio = { version = "^1", features = ["time"] }

src/mutation.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ use dioxus_lib::{
1616
hooks::{use_memo, use_reactive},
1717
signals::CopyValue,
1818
};
19+
#[cfg(not(target_family = "wasm"))]
20+
use tokio::time;
21+
#[cfg(not(target_family = "wasm"))]
1922
use tokio::time::Instant;
23+
#[cfg(target_family = "wasm")]
24+
use wasmtimer::tokio as time;
25+
#[cfg(target_family = "wasm")]
26+
use web_time::Instant;
2027

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

187194
// Finally clear the mutation
188195
let mut storage = storage_clone.write();

src/query.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ use dioxus_lib::{
1919
signals::CopyValue,
2020
};
2121
use futures_util::stream::{FuturesUnordered, StreamExt};
22-
use tokio::{sync::Notify, time::Instant};
22+
use tokio::sync::Notify;
23+
#[cfg(not(target_family = "wasm"))]
24+
use tokio::time;
25+
#[cfg(not(target_family = "wasm"))]
26+
use tokio::time::Instant;
27+
#[cfg(target_family = "wasm")]
28+
use wasmtimer::tokio as time;
29+
#[cfg(target_family = "wasm")]
30+
use web_time::Instant;
2331

2432
pub trait QueryCapability
2533
where
@@ -222,7 +230,7 @@ impl<Q: QueryCapability> QueriesStorage<Q> {
222230
let task = spawn_forever(async move {
223231
loop {
224232
// Wait as long as the stale time is configured
225-
tokio::time::sleep(interval).await;
233+
time::sleep(interval).await;
226234

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

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

306-
// Notify the suspense task if any
307-
if let Some(suspense_task) = &*query_data.suspense_task.borrow() {
308-
suspense_task.notifier.notify_waiters();
309-
};
313+
// Notify the suspense task if any
314+
if let Some(suspense_task) = &*query_data.suspense_task.borrow() {
315+
suspense_task.notifier.notify_waiters();
316+
};
317+
}
310318

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

317325
// Finally clear the query
318326
let mut storage = storage.storage.write();
@@ -387,6 +395,11 @@ impl<Q: QueryCapability> QueriesStorage<Q> {
387395
for reactive_context in query_data.reactive_contexts.lock().unwrap().iter() {
388396
reactive_context.mark_dirty();
389397
}
398+
399+
// Notify the suspense task if any
400+
if let Some(suspense_task) = &*query_data.suspense_task.borrow() {
401+
suspense_task.notifier.notify_waiters();
402+
};
390403
}));
391404
}
392405

0 commit comments

Comments
 (0)