mirror of
https://github.com/Zenithsiz/dynatos.git
synced 2026-02-04 10:21:23 +00:00
Extracted NodeDynText::with_dyn_text to a new trait.
This commit is contained in:
parent
eeecefa35d
commit
c4d52a8023
@ -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,
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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},
|
||||
|
||||
@ -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},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user