Synphonyte / leptos-use

Collection of essential Leptos utilities inspired by React-Use / VueUse
https://leptos-use.rs/
Apache License 2.0
328 stars 72 forks source link

Feature request: Better reporting for Reactivity functions when used outside a reactive tracking context #129

Closed bicarlsen closed 3 months ago

bicarlsen commented 3 months ago

If a signal_debounced (and I assume signal_throttled but haven't verfied this) is used outside a reactive tracking context, the warning gives the signal's definition location as leptos-use-0.10.10/src/signal_debounced.rs:6:1 instead of the location where signal_debounced is being called.

Ideally the location of the signal_debounced call would be set as the definition location of the signal.

maccesch commented 3 months ago

That sounds very resonable but I have no idea how to archieve this unfortunately. Do you have any pointers?

bicarlsen commented 3 months ago

Looking at the source for create_signal it looks like we would need to patch the doesn't seem we have access to the defined_at field. Unfortunately, it doesn't look like we have access to it currently.

Maybe @gbj or @benwis can give some insight.

gbj commented 3 months ago

Adding #[track_caller] to this and similar function definitions should work: https://github.com/Synphonyte/leptos-use/blob/9c035d50d8fb9265a5d2761af57372e5c45f3ecd/src/utils/signal_filtered.rs#L10

Basically signal creation uses std::panic::Location::caller(). Calling that returns the location where... well, it was called. But if you add #[track_caller] it basically says "ignore the current function definition, and move up the stack". So the "Defined at" location is roughly "the last nested function call that wasn't marked #[track_caller]"

Does that explanation make sense? If not I can expand on it. Basically I would assume most fn definitions in a library like this that create signals somewhere inside them should be marked #[track_caller]

benwis commented 3 months ago

He beat me to it!

maccesch commented 3 months ago

Thanks, that sounds good!

bicarlsen commented 3 months ago

Sweet! @maccesch, would you like me to do the PR, or are you handling it?

Thanks @gbj and @maccesch for all the great help :)

maccesch commented 3 months ago

Yeah if you could do a PR that would be awesome!