-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add aper-leptos #46
Add aper-leptos #46
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "aper-leptos" | ||
version = "0.5.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
aper = {path = "../aper"} | ||
aper-websocket-client = { version="0.5.0", path="../aper-websocket-client" } | ||
leptos = { version = "0.6.14", features = ["csr"] } | ||
serde = "1.0.210" | ||
tracing-subscriber = "0.3.18" | ||
tracing-web = "0.1.3" |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,26 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
use aper::{data_structures::Atom, AperSync}; | ||||||||||||||||||||||||||||||||||||||||||||||
use leptos::{create_signal, SignalGet, SignalSet}; | ||||||||||||||||||||||||||||||||||||||||||||||
use serde::{de::DeserializeOwned, Serialize}; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
pub mod init_tracing; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
pub trait Watch<T> { | ||||||||||||||||||||||||||||||||||||||||||||||
fn watch(&self) -> Box<dyn Fn() -> T>; | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
impl<T> Watch<T> for Atom<T> | ||||||||||||||||||||||||||||||||||||||||||||||
where | ||||||||||||||||||||||||||||||||||||||||||||||
T: Serialize + DeserializeOwned + Default + Clone + Send + Sync + 'static, | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
fn watch(&self) -> Box<dyn Fn() -> T> { | ||||||||||||||||||||||||||||||||||||||||||||||
let (signal, set_signal) = create_signal(self.get()); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
let self_clone = self.clone(); | ||||||||||||||||||||||||||||||||||||||||||||||
self.listen(move || { | ||||||||||||||||||||||||||||||||||||||||||||||
set_signal.set(self_clone.get()); | ||||||||||||||||||||||||||||||||||||||||||||||
true | ||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Box::new(move || signal.get()) | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Update the implementation to return By returning the Apply this diff to update the implementation: fn watch(&self) -> ReadSignal<T> {
let (signal, set_signal) = create_signal(self.get());
let self_clone = self.clone();
self.listen(move || {
set_signal.set(self_clone.get());
true
});
- Box::new(move || signal.get())
+ signal
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider returning
ReadSignal<T>
instead ofBox<dyn Fn() -> T>
.Returning a
ReadSignal<T>
directly is more idiomatic in Leptos and allows consumers to leverage the full capabilities of signals, including reactive updates and integration with the Leptos ecosystem. This also removes the need to box a closure, improving performance and type safety.Apply this diff to change the return type in the trait definition: