Extracted NodeDynText::with_dyn_text to a new trait.

This commit is contained in:
Filipe Rodrigues 2024-02-19 04:31:59 +00:00
parent eeecefa35d
commit c4d52a8023
Signed by: zenithsiz
SSH Key Fingerprint: SHA256:Mb5ppb3Sh7IarBO/sBTXLHbYEOz37hJAlslLQPPAPaU
4 changed files with 18 additions and 14 deletions

View File

@ -15,7 +15,7 @@ mod object_dyn_prop;
pub use self::{
element_dyn_attr::{ElementDynAttr, ElementWithDynAttr},
node_dyn_child::{NodeDynChild, NodeWithDynChild, ToDynNode},
node_dyn_text::{NodeDynText, WithDynText},
node_dyn_text::{NodeDynText, NodeWithDynText, WithDynText},
object_attach_context::{ObjectAttachContext, ObjectWithContext},
object_attach_effect::{ObjectAttachEffect, ObjectWithEffect},
object_dyn_prop::ObjectDynProp,

View File

@ -9,20 +9,17 @@ use {
/// Extension trait to add reactive text to a node
#[extend::ext(name = NodeDynText)]
pub impl<T> T
where
T: AsRef<web_sys::Node>,
{
pub impl web_sys::Node {
/// Adds dynamic text to this node
fn set_dyn_text<U>(&self, text: U)
fn set_dyn_text<T>(&self, text: T)
where
U: WithDynText + 'static,
T: WithDynText + 'static,
{
// Create the value to attach
// Note: It's important that we only keep a `WeakRef` to the node.
// Otherwise, the node will be keeping us alive, while we keep
// the node alive, causing a leak.
let node = WeakRef::new(self.as_ref());
let node = WeakRef::new(self);
let text_effect = Effect::try_new(move || {
// Try to get the node
let node = node.get().or_return()?;
@ -33,17 +30,24 @@ where
.or_return()?;
// Then set it
self.as_ref().attach_effect(text_effect);
self.attach_effect(text_effect);
}
}
/// Extension trait to add reactive text to a node
#[extend::ext(name = NodeWithDynText)]
pub impl<N> N
where
N: AsRef<web_sys::Node>,
{
/// Adds dynamic text to this node.
///
/// Returns the node, for chaining
fn with_dyn_text<U>(self, text: U) -> Self
fn with_dyn_text<T>(self, text: T) -> Self
where
U: WithDynText + 'static,
T: WithDynText + 'static,
{
self.set_dyn_text(text);
self.as_ref().set_dyn_text(text);
self
}
}

View File

@ -2,7 +2,7 @@
// Imports
use {
dynatos::{ElementWithDynAttr, NodeDynText},
dynatos::{ElementWithDynAttr, NodeWithDynText},
dynatos_html::{html, NodeWithChildren, NodeWithText},
dynatos_reactive::{Signal, SignalGet, SignalSet, SignalUpdate},
dynatos_util::{ev, EventTargetWithListener, JsResultContext},

View File

@ -5,7 +5,7 @@
// Imports
use {
dynatos::{NodeDynText, ObjectWithContext},
dynatos::{NodeWithDynText, ObjectWithContext},
dynatos_html::{html, NodeWithChildren, NodeWithText},
dynatos_reactive::{SignalGet, SignalSet, SignalUpdate, SignalWithDefault},
dynatos_router::{Location, QuerySignal},