Skip to content
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

Merged
merged 3 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
999 changes: 935 additions & 64 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ exclude = [
]

members = [
"aper",
"aper/aper-derive*",
"aper-websocket-client",
"aper-leptos",
"aper-serve",
"aper-stateroom",
"site/doctests", "aper-yew",
"aper-websocket-client",
"aper-yew",
"aper",
"aper/aper-derive*",
"site/doctests",
]
12 changes: 12 additions & 0 deletions aper-leptos/Cargo.toml
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"
26 changes: 26 additions & 0 deletions aper-leptos/src/lib.rs
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>;
}
Copy link

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 of Box<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:

+use leptos::ReadSignal;

 pub trait Watch<T> {
-    fn watch(&self) -> Box<dyn Fn() -> T>;
+    fn watch(&self) -> ReadSignal<T>;
 }

Committable suggestion was skipped due to low confidence.


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())
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Update the implementation to return ReadSignal<T> directly.

By returning the ReadSignal<T> from create_signal, you simplify the code and provide a more flexible interface for the consumers of the watch method.

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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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())
}
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
});
signal
}

}
Loading
Loading