Skip to content

Commit

Permalink
feat: 0.6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Dec 10, 2024
1 parent 5aeaa6a commit 8c4cde0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 60 deletions.
7 changes: 4 additions & 3 deletions 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.5.1"
version = "0.6.0"
edition = "2021"
license = "MIT"
authors = ["Marc Espín <mespinsanz@gmail.com>"]
Expand All @@ -12,10 +12,11 @@ keywords = ["dioxus", "async", "state", "synchronization"]
categories = ["gui", "asynchronous"]

[dependencies]
dioxus-lib = { version = "0.5", default-features = false, features = ["macro", "hooks", "signals"] }
dioxus-lib = { version = "0.6", default-features = false, features = ["macro", "hooks", "signals"] }
futures-util = "0.3.28"
instant = { version = "0.1", features = ["wasm-bindgen"] }
warnings = "0.2.1"

[dev-dependencies]
dioxus = { version = "0.5", features = ["desktop"]}
dioxus = { version = "0.6", features = ["desktop"] }
tokio = { version = "1.29.1", features = ["time"] }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ See the [Docs](https://docs.rs/dioxus-query/latest/dioxus_query/) or join the [D

## Support

- **Dioxus v0.5** 🧬
- **Dioxus v0.6** 🧬
- All renderers ([web](https://dioxuslabs.com/learn/0.4/getting_started/wasm), [desktop](https://dioxuslabs.com/learn/0.4/getting_started/desktop), [freya](https://github.com/marc2332/freya), etc)
- Both WASM and native targets

Expand Down
123 changes: 67 additions & 56 deletions src/use_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ use dioxus_lib::prelude::*;
use futures_util::Future;
use std::{
any::TypeId,
cell::RefCell,
collections::HashSet,
hash::Hash,
rc::Rc,
sync::{Arc, RwLock, RwLockReadGuard},
};
mod warnings {
pub use warnings::Warning;
}
pub use warnings::Warning;

use crate::{
cached_result::CachedResult,
Expand Down Expand Up @@ -62,25 +68,27 @@ where

impl<T, E, K: Eq + Hash> Drop for UseQueryCleaner<T, E, K> {
fn drop(&mut self) {
let mut queries_registry = match self.client.queries_registry.try_write_unchecked() {
Err(dioxus_lib::prelude::BorrowMutError::Dropped(_)) => {
// It's safe to skip this error as the RadioStation's signals could have been dropped before the caller of this function.
// For instance: If you closed the app, the RadioStation would be dropped along all it's signals, causing the inner components
// to still have dropped signals and thus causing this error if they were to call the signals on a custom destructor.
return;
dioxus_lib::prelude::warnings::signal_write_in_component_body::allow(|| {
let mut queries_registry = match self.client.queries_registry.try_write_unchecked() {
Err(dioxus_lib::prelude::BorrowMutError::Dropped(_)) => {
// It's safe to skip this error as the RadioStation's signals could have been dropped before the caller of this function.
// For instance: If you closed the app, the RadioStation would be dropped along all it's signals, causing the inner components
// to still have dropped signals and thus causing this error if they were to call the signals on a custom destructor.
return;
}
Err(e) => panic!("Unexpected error: {e}"),
Ok(v) => v,
};

let query_listeners = queries_registry.get_mut(&self.registry_entry).unwrap();
// Remove this listener
query_listeners.listeners.remove(&self.scope_id);

// Clear the queries registry of this listener if it was the last one
if query_listeners.listeners.is_empty() {
queries_registry.remove(&self.registry_entry);
}
Err(e) => panic!("Unexpected error: {e}"),
Ok(v) => v,
};

let query_listeners = queries_registry.get_mut(&self.registry_entry).unwrap();
// Remove this listener
query_listeners.listeners.remove(&self.scope_id);

// Clear the queries registry of this listener if it was the last one
if query_listeners.listeners.is_empty() {
queries_registry.remove(&self.registry_entry);
}
});
}
}

Expand Down Expand Up @@ -135,45 +143,48 @@ where
query.registry_entry.query_keys = query_keys.to_vec();

let registry_entry = query.registry_entry;
let mut queries_registry = client.queries_registry.write_unchecked();

// Create a group of listeners for the given [RegistryEntry] key.
let query_listeners =
queries_registry
.entry(registry_entry.clone())
.or_insert(QueryListeners {
listeners: HashSet::default(),
value: QueryValue::new(RwLock::new(CachedResult::new(
query.initial_value.unwrap_or_default(),
))),
query_fn: query.query_fn,
});

// Register this listener's scope
query_listeners
.listeners
.insert(current_scope_id().unwrap());

let value = query_listeners.value.clone();

// Asynchronously initialize the query value
spawn({
to_owned![registry_entry];
async move {
client.run_new_query(&registry_entry).await;
}
});

UseQuery {
client,
value,
scope_id: current_scope_id().unwrap(),
cleaner: Signal::new(UseQueryCleaner {
dioxus_lib::prelude::warnings::signal_write_in_component_body::allow(|| {
let mut queries_registry = client.queries_registry.write_unchecked();

// Create a group of listeners for the given [RegistryEntry] key.
let query_listeners =
queries_registry
.entry(registry_entry.clone())
.or_insert(QueryListeners {
listeners: HashSet::default(),
value: QueryValue::new(RwLock::new(CachedResult::new(
query.initial_value.unwrap_or_default(),
))),
query_fn: query.query_fn,
});

// Register this listener's scope
query_listeners
.listeners
.insert(current_scope_id().unwrap());

let value = query_listeners.value.clone();

// Asynchronously initialize the query value
spawn({
to_owned![registry_entry];
async move {
client.run_new_query(&registry_entry).await;
}
});

UseQuery {
client,
registry_entry,
value,
scope_id: current_scope_id().unwrap(),
}),
}
cleaner: Signal::new(UseQueryCleaner {
client,
registry_entry,
scope_id: current_scope_id().unwrap(),
}),
}
})
})
}

Expand All @@ -190,8 +201,8 @@ fn use_sync_memo<T: 'static + Clone, D: PartialEq + 'static>(
value: T,
deps: D,
}
let mut value = use_signal::<Option<Memoized<T, D>>>(|| None);
let mut memoized_value = value.write();
let value = use_hook::<Rc<RefCell<Option<Memoized<T, D>>>>>(|| Rc::default());
let mut memoized_value = value.borrow_mut();

let deps_have_changed = memoized_value
.as_ref()
Expand Down

0 comments on commit 8c4cde0

Please sign in to comment.