diff --git a/dynatos/src/lib.rs b/dynatos/src/lib.rs index f5373cc..36f5304 100644 --- a/dynatos/src/lib.rs +++ b/dynatos/src/lib.rs @@ -18,5 +18,5 @@ pub use self::{ node_dyn_text::{NodeDynText, NodeWithDynText, WithDynText}, object_attach_context::{ObjectAttachContext, ObjectWithContext}, object_attach_effect::{ObjectAttachEffect, ObjectWithEffect}, - object_dyn_prop::ObjectDynProp, + object_dyn_prop::{ObjectDynProp, ObjectWithDynProp}, }; diff --git a/dynatos/src/object_dyn_prop.rs b/dynatos/src/object_dyn_prop.rs index bb28a32..f8a8f9a 100644 --- a/dynatos/src/object_dyn_prop.rs +++ b/dynatos/src/object_dyn_prop.rs @@ -10,10 +10,7 @@ use { /// Extension trait to add reactive prop to an object #[extend::ext(name = ObjectDynProp)] -pub impl T -where - T: AsRef, -{ +pub impl js_sys::Object { /// Adds a dynamic property to this object fn add_dyn_prop(&self, f: F) where @@ -25,7 +22,7 @@ where // Note: It's important that we only keep a `WeakRef` to the object. // Otherwise, the object will be keeping us alive, while we keep // the object alive, causing a leak. - let object = WeakRef::new(self.as_ref()); + let object = WeakRef::new(self); let prop_effect = Effect::try_new(move || { // Try to get the object let object = object.get().or_return()?; @@ -45,20 +42,7 @@ where .or_return()?; // Then set it - self.as_ref().attach_effect(prop_effect); - } - - /// Adds a dynamic property to this object. - /// - /// Returns the object, for chaining - fn with_dyn_prop(self, f: F) -> Self - where - F: Fn() -> (K, Option) + 'static, - K: AsRef, - V: Into, - { - self.add_dyn_prop(f); - self + self.attach_effect(prop_effect); } /// Adds a dynamic property to this object, where only the value is dynamic. @@ -70,6 +54,26 @@ where { self.add_dyn_prop(move || (key, f())); } +} + +/// Extension trait to add reactive prop to an object +#[extend::ext(name = ObjectWithDynProp)] +pub impl O +where + O: AsRef, +{ + /// Adds a dynamic property to this object. + /// + /// Returns the object, for chaining + fn with_dyn_prop(self, f: F) -> Self + where + F: Fn() -> (K, Option) + 'static, + K: AsRef, + V: Into, + { + self.as_ref().add_dyn_prop(f); + self + } /// Adds a dynamic property to this object, where only the value is dynamic. /// @@ -80,7 +84,7 @@ where K: AsRef + Copy + 'static, V: Into, { - self.add_dyn_prop_value(key, f); + self.as_ref().add_dyn_prop_value(key, f); self } }