fix memo and resource caller information (#2443)

This commit is contained in:
Evan Almloff authored and GitHub committed 2024-06-06 17:01:42 -07:00
1 parent 489758d5ba
commit 5e57779435
3 files changed
+36 -10

No files matched your search

+15 -8
View File
@@ -5,7 +5,6 @@ use crate::{CopyValue, ReadOnlySignal};
use std::{
cell::RefCell,
ops::Deref,
panic::Location,
sync::{atomic::AtomicBool, Arc},
};
@@ -36,7 +35,18 @@ where
impl<T: 'static> Memo<T> {
/// Create a new memo
#[track_caller]
pub fn new(mut f: impl FnMut() -> T + 'static) -> Self
pub fn new(f: impl FnMut() -> T + 'static) -> Self
where
T: PartialEq,
{
Self::new_with_location(f, std::panic::Location::caller())
}
/// Create a new memo with an explicit location
pub fn new_with_location(
mut f: impl FnMut() -> T + 'static,
location: &'static std::panic::Location<'static>,
) -> Self
where
T: PartialEq,
{
@@ -50,11 +60,8 @@ impl<T: 'static> Memo<T> {
let _ = tx.unbounded_send(());
}
};
let rc = ReactiveContext::new_with_callback(
callback,
current_scope_id().unwrap(),
Location::caller(),
);
let rc =
ReactiveContext::new_with_callback(callback, current_scope_id().unwrap(), location);
// Create a new signal in that context, wiring up its dependencies and subscribers
let mut recompute = move || rc.run_in(&mut f);
@@ -64,7 +71,7 @@ impl<T: 'static> Memo<T> {
dirty,
callback: recompute,
});
let state: Signal<T> = Signal::new(value);
let state: Signal<T> = Signal::new_with_caller(value, location);
let memo = Memo {
inner: state,
+10
View File
@@ -91,6 +91,16 @@ impl<T: PartialEq + 'static> Signal<T> {
pub fn memo(f: impl FnMut() -> T + 'static) -> Memo<T> {
Memo::new(f)
}
/// Creates a new unsync Selector with an explicit location. The selector will be run immediately and whenever any signal it reads changes.
///
/// Selectors can be used to efficiently compute derived data from signals.
pub fn memo_with_location(
f: impl FnMut() -> T + 'static,
location: &'static std::panic::Location<'static>,
) -> Memo<T> {
Memo::new_with_location(f, location)
}
}
impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {