From 6fc59843440dfc7af88e50ced8f499cbcb5d6bb4 Mon Sep 17 00:00:00 2001 From: Filipe Rodrigues Date: Mon, 19 Feb 2024 04:26:39 +0000 Subject: [PATCH] Extracted `ObjectAttachEffect::with_effect` to a new trait. --- dynatos/src/lib.rs | 4 ++-- dynatos/src/object_attach_context.rs | 26 +++++++++++++++----------- dynatos/src/object_attach_effect.rs | 19 +++++++++++-------- examples/query/src/main.rs | 2 +- examples/router/src/main.rs | 2 +- 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/dynatos/src/lib.rs b/dynatos/src/lib.rs index 5b958c3..9320a67 100644 --- a/dynatos/src/lib.rs +++ b/dynatos/src/lib.rs @@ -16,7 +16,7 @@ pub use self::{ element_dyn_attr::ElementDynAttr, node_dyn_child::{NodeDynChild, ToDynNode}, node_dyn_text::{NodeDynText, WithDynText}, - object_attach_context::ObjectAttachContext, - object_attach_effect::ObjectAttachEffect, + object_attach_context::{ObjectAttachContext, ObjectWithContext}, + object_attach_effect::{ObjectAttachEffect, ObjectWithEffect}, object_dyn_prop::ObjectDynProp, }; diff --git a/dynatos/src/object_attach_context.rs b/dynatos/src/object_attach_context.rs index 547b538..55feb8f 100644 --- a/dynatos/src/object_attach_context.rs +++ b/dynatos/src/object_attach_context.rs @@ -10,35 +10,39 @@ use { /// Extension trait to add an context to an object // TODO: Allow removing context handles? #[extend::ext(name = ObjectAttachContext)] -pub impl T -where - T: AsRef, -{ +pub impl js_sys::Object { /// Provides and attaches a context to this object - fn attach_context(&self, value: U) { + fn attach_context(&self, value: T) { // Get the context handles array, or create it, if it doesn't exist // TODO: Use an static anonymous symbol? let prop_name: &str = "__dynatos_ctx_handles"; - let obj = self.as_ref(); - let ctx_handles = match obj.get::(prop_name) { + let ctx_handles = match self.get::(prop_name) { Ok(ctx_handles) => ctx_handles, Err(dynatos_util::GetError::WrongType(err)) => panic!("Contexts array was the wrong type: {err:?}"), Err(dynatos_util::GetError::Missing) => { let ctx_handles = js_sys::Array::new(); - obj.set_prop(prop_name, &ctx_handles); + self.set_prop(prop_name, &ctx_handles); ctx_handles }, }; // Then push the context handle - let handle = dynatos_context::provide::(value).into_opaque(); + let handle = dynatos_context::provide::(value).into_opaque(); let handle = WasmContextHandle(handle); ctx_handles.push(&handle.into()); } +} +/// Extension trait to add an context to an object +// TODO: Allow removing context handles? +#[extend::ext(name = ObjectWithContext)] +pub impl O +where + O: AsRef, +{ /// Provides and attaches a context to this object - fn with_context(self, value: U) -> Self { - self.attach_context(value); + fn with_context(self, value: T) -> Self { + self.as_ref().attach_context(value); self } } diff --git a/dynatos/src/object_attach_effect.rs b/dynatos/src/object_attach_effect.rs index 3b660ec..4ba1054 100644 --- a/dynatos/src/object_attach_effect.rs +++ b/dynatos/src/object_attach_effect.rs @@ -10,22 +10,18 @@ use { /// Extension trait to add an effect to an object // TODO: Allow removing effects? #[extend::ext(name = ObjectAttachEffect)] -pub impl T -where - T: AsRef, -{ +pub impl js_sys::Object { /// Attaches an effect to this object fn attach_effect(&self, effect: Effect) { // Get the effects map, or create it, if it doesn't exist // TODO: Use an static anonymous symbol? let prop_name: &str = "__dynatos_effects"; - let obj = self.as_ref(); - let effects = match obj.get::(prop_name) { + let effects = match self.get::(prop_name) { Ok(effects) => effects, Err(dynatos_util::GetError::WrongType(err)) => panic!("Effects map was the wrong type: {err:?}"), Err(dynatos_util::GetError::Missing) => { let effects = js_sys::Map::new(); - obj.set_prop(prop_name, &effects); + self.set_prop(prop_name, &effects); effects }, }; @@ -35,12 +31,19 @@ where let effect = WasmEffect(effect); effects.set(&effect_key.into(), &effect.into()); } +} +/// Extension trait to add an effect to an object +#[extend::ext(name = ObjectWithEffect)] +pub impl O +where + O: AsRef, +{ /// Attaches an effect to this object. /// /// Returns the object, for chaining fn with_effect(self, effect: Effect) -> Self { - self.attach_effect(effect); + self.as_ref().attach_effect(effect); self } } diff --git a/examples/query/src/main.rs b/examples/query/src/main.rs index f5de03c..29f82b4 100644 --- a/examples/query/src/main.rs +++ b/examples/query/src/main.rs @@ -5,7 +5,7 @@ // Imports use { - dynatos::{NodeDynText, ObjectAttachContext}, + dynatos::{NodeDynText, ObjectWithContext}, dynatos_html::{html, NodeWithChildren, NodeWithText}, dynatos_reactive::{SignalGet, SignalSet, SignalUpdate, SignalWithDefault}, dynatos_router::{Location, QuerySignal}, diff --git a/examples/router/src/main.rs b/examples/router/src/main.rs index 7b186f8..02874bd 100644 --- a/examples/router/src/main.rs +++ b/examples/router/src/main.rs @@ -5,7 +5,7 @@ // Imports use { - dynatos::{NodeDynChild, ObjectAttachContext}, + dynatos::{NodeDynChild, ObjectWithContext}, dynatos_html::{html, NodeWithChildren, NodeWithText}, dynatos_reactive::SignalGet, dynatos_router::Location,