Preserve owner in Actions and event listeners (#4267)

* Preserve Owner for Action's, allowing context retrieval

* Preserve owner for event listeners, allowing context retrieval

* CI

* Refactor

---------

Co-authored-by: Zak Stucke <zakstucke@hotmail.c.uk>
This commit is contained in:
zakstucke
2025-08-29 02:50:33 +03:00
committed by GitHub
parent 1af5f66ee6
commit 7adb11ec49
2 changed files with 32 additions and 4 deletions

View File

@@ -110,6 +110,8 @@ where
{
On {
event,
#[cfg(feature = "reactive_graph")]
owner: reactive_graph::owner::Owner::current().unwrap_or_default(),
cb: Some(SendWrapper::new(cb)),
}
}
@@ -135,6 +137,8 @@ where
/// An [`Attribute`] that adds an event listener to an element.
pub struct On<E, F> {
event: E,
#[cfg(feature = "reactive_graph")]
owner: reactive_graph::owner::Owner,
cb: Option<SendWrapper<F>>,
}
@@ -146,6 +150,8 @@ where
fn clone(&self) -> Self {
Self {
event: self.event.clone(),
#[cfg(feature = "reactive_graph")]
owner: self.owner.clone(),
cb: self.cb.clone(),
}
}
@@ -193,6 +199,10 @@ where
let _tracing_guard = span.enter();
let ev = E::EventType::from(ev);
#[cfg(feature = "reactive_graph")]
self.owner.with(|| cb.invoke(ev));
#[cfg(not(feature = "reactive_graph"))]
cb.invoke(ev);
}) as Box<dyn FnMut(crate::renderer::types::Event)>;
@@ -232,6 +242,10 @@ where
let _tracing_guard = span.enter();
let ev = E::EventType::from(ev);
#[cfg(feature = "reactive_graph")]
self.owner.with(|| cb.invoke(ev));
#[cfg(not(feature = "reactive_graph"))]
cb.invoke(ev);
}) as Box<dyn FnMut(crate::renderer::types::Event)>;
@@ -320,6 +334,8 @@ where
fn into_cloneable(self) -> Self::Cloneable {
On {
cb: self.cb.map(|cb| SendWrapper::new(cb.take().into_shared())),
#[cfg(feature = "reactive_graph")]
owner: self.owner,
event: self.event,
}
}
@@ -327,6 +343,8 @@ where
fn into_cloneable_owned(self) -> Self::CloneableOwned {
On {
cb: self.cb.map(|cb| SendWrapper::new(cb.take().into_shared())),
#[cfg(feature = "reactive_graph")]
owner: self.owner,
event: self.event,
}
}