Skip to content

Commit

Permalink
Debug NodeRef Warning (#2414)
Browse files Browse the repository at this point in the history
In production #[repr(transparent)] is use to ensure a compact representation in memory.
In developement the memory is slightly expanded.
  • Loading branch information
martinfrances107 committed Mar 25, 2024
1 parent f3d19ca commit 42cbdf2
Showing 1 changed file with 80 additions and 36 deletions.
116 changes: 80 additions & 36 deletions leptos_dom/src/node_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,76 @@ use leptos_reactive::{
};
use std::cell::Cell;

/// Contains a shared reference to a DOM node created while using the `view`
/// macro to create your UI.
///
/// ```
/// # use leptos::{*, logging::log};
/// use leptos::html::Input;
///
/// #[component]
/// pub fn MyComponent() -> impl IntoView {
/// let input_ref = create_node_ref::<Input>();
///
/// let on_click = move |_| {
/// let node =
/// input_ref.get().expect("input_ref should be loaded by now");
/// // `node` is strongly typed
/// // it is dereferenced to an `HtmlInputElement` automatically
/// log!("value is {:?}", node.value())
/// };
///
/// view! {
/// <div>
/// // `node_ref` loads the input
/// <input _ref=input_ref type="text"/>
/// // the button consumes it
/// <button on:click=on_click>"Click me"</button>
/// </div>
/// }
/// }
/// ```
#[repr(transparent)]
pub struct NodeRef<T: ElementDescriptor + 'static>(
RwSignal<Option<HtmlElement<T>>>,
cfg_if::cfg_if!(
if #[cfg(debug_assertions)] {
/// Contains a shared reference to a DOM node created while using the `view`
/// macro to create your UI.
///
/// ```
/// # use leptos::{*, logging::log};
/// use leptos::html::Input;
///
/// #[component]
/// pub fn MyComponent() -> impl IntoView {
/// let input_ref = create_node_ref::<Input>();
///
/// let on_click = move |_| {
/// let node =
/// input_ref.get().expect("input_ref should be loaded by now");
/// // `node` is strongly typed
/// // it is dereferenced to an `HtmlInputElement` automatically
/// log!("value is {:?}", node.value())
/// };
///
/// view! {
/// <div>
/// // `node_ref` loads the input
/// <input _ref=input_ref type="text"/>
/// // the button consumes it
/// <button on:click=on_click>"Click me"</button>
/// </div>
/// }
/// }
/// ```
pub struct NodeRef<T: ElementDescriptor + 'static> {
defined_at: &'static std::panic::Location<'static>,
element: RwSignal<Option<HtmlElement<T>>>,
}
} else {
/// Contains a shared reference to a DOM node created while using the `view`
/// macro to create your UI.
///
/// ```
/// # use leptos::{*, logging::log};
/// use leptos::html::Input;
///
/// #[component]
/// pub fn MyComponent() -> impl IntoView {
/// let input_ref = create_node_ref::<Input>();
///
/// let on_click = move |_| {
/// let node =
/// input_ref.get().expect("input_ref should be loaded by now");
/// // `node` is strongly typed
/// // it is dereferenced to an `HtmlInputElement` automatically
/// log!("value is {:?}", node.value())
/// };
///
/// view! {
/// <div>
/// // `node_ref` loads the input
/// <input _ref=input_ref type="text"/>
/// // the button consumes it
/// <button on:click=on_click>"Click me"</button>
/// </div>
/// }
/// }
/// ```
#[repr(transparent)]
pub struct NodeRef<T: ElementDescriptor + 'static> {
element: RwSignal<Option<HtmlElement<T>>>,
}
}
);

/// Creates a shared reference to a DOM node created while using the `view`
Expand Down Expand Up @@ -67,9 +105,13 @@ pub struct NodeRef<T: ElementDescriptor + 'static>(
/// }
/// }
/// ```
#[track_caller]
#[inline(always)]
pub fn create_node_ref<T: ElementDescriptor + 'static>() -> NodeRef<T> {
NodeRef(create_rw_signal(None))
NodeRef {
defined_at: std::panic::Location::caller(),
element: create_rw_signal(None),
}
}

impl<T: ElementDescriptor + 'static> NodeRef<T> {
Expand Down Expand Up @@ -122,7 +164,7 @@ impl<T: ElementDescriptor + 'static> NodeRef<T> {
where
T: Clone,
{
self.0.get()
self.element.get()
}

/// Gets the element that is currently stored in the reference.
Expand All @@ -134,7 +176,7 @@ impl<T: ElementDescriptor + 'static> NodeRef<T> {
where
T: Clone,
{
self.0.get_untracked()
self.element.get_untracked()
}

#[doc(hidden)]
Expand All @@ -146,7 +188,7 @@ impl<T: ElementDescriptor + 'static> NodeRef<T> {
where
T: Clone,
{
self.0.update(|current| {
self.element.update(|current| {
if current.is_some() {
crate::debug_warn!(
"You are setting a NodeRef that has already been filled. \
Expand Down Expand Up @@ -178,6 +220,7 @@ impl<T: ElementDescriptor + 'static> NodeRef<T> {
}

impl<T: ElementDescriptor> Clone for NodeRef<T> {
#[track_caller]
fn clone(&self) -> Self {
*self
}
Expand All @@ -186,6 +229,7 @@ impl<T: ElementDescriptor> Clone for NodeRef<T> {
impl<T: ElementDescriptor + 'static> Copy for NodeRef<T> {}

impl<T: ElementDescriptor + 'static> Default for NodeRef<T> {
#[track_caller]
fn default() -> Self {
Self::new()
}
Expand Down

0 comments on commit 42cbdf2

Please sign in to comment.