Change default behavior of WriteSignal so that set_X() assigns a new value T, and set_X.update(...) takes a fn

This commit is contained in:
Greg Johnston 2022-10-08 20:38:34 -04:00
parent 1a61b7a5f9
commit a26ab79415
8 changed files with 26 additions and 29 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "leptos_reactive"
version = "0.0.3"
version = "0.0.4"
edition = "2021"
authors = ["Greg Johnston"]
license = "MIT"

View file

@ -106,7 +106,7 @@ mod tests {
acc.set(r());
}
});
w(|n| *n += 1);
w.update(|n| *n += 1);
}
})
})

View file

@ -15,7 +15,7 @@ where
create_isomorphic_effect(cx, move |prev| {
let new = f(prev.clone());
if prev.as_ref() != Some(&new) {
set(|n| *n = Some(new.clone()));
set(Some(new.clone()));
}
new
});

View file

@ -27,13 +27,13 @@ use crate::{create_effect, create_signal, ReadSignal, Scope, WriteSignal};
///
/// assert_eq!(is_selected(5), false);
/// assert_eq!(*total_notifications.borrow(), 0);
/// set_a(|n| *n = 5);
/// set_a(5);
/// assert_eq!(is_selected(5), true);
/// assert_eq!(*total_notifications.borrow(), 1);
/// set_a(|n| *n = 5);
/// set_a(5);
/// assert_eq!(is_selected(5), true);
/// assert_eq!(*total_notifications.borrow(), 1);
/// set_a(|n| *n = 4);
/// set_a(4);
/// assert_eq!(is_selected(5), false);
/// //assert_eq!(*total_notifications.borrow(), 2);
/// # })

View file

@ -214,35 +214,32 @@ where
impl<T> Copy for WriteSignal<T> where T: Clone {}
impl<T, F> FnOnce<(F,)> for WriteSignal<T>
impl<T> FnOnce<(T,)> for WriteSignal<T>
where
F: Fn(&mut T),
T: Clone + 'static,
{
type Output = ();
extern "rust-call" fn call_once(self, args: (F,)) -> Self::Output {
self.update(args.0)
extern "rust-call" fn call_once(self, args: (T,)) -> Self::Output {
self.update(move |n| *n = args.0)
}
}
impl<T, F> FnMut<(F,)> for WriteSignal<T>
impl<T> FnMut<(T,)> for WriteSignal<T>
where
F: Fn(&mut T),
T: Clone + 'static,
{
extern "rust-call" fn call_mut(&mut self, args: (F,)) -> Self::Output {
self.update(args.0)
extern "rust-call" fn call_mut(&mut self, args: (T,)) -> Self::Output {
self.update(move |n| *n = args.0)
}
}
impl<T, F> Fn<(F,)> for WriteSignal<T>
impl<T> Fn<(T,)> for WriteSignal<T>
where
F: Fn(&mut T),
T: Clone + 'static,
{
extern "rust-call" fn call(&self, args: (F,)) -> Self::Output {
self.update(args.0)
extern "rust-call" fn call(&self, args: (T,)) -> Self::Output {
self.update(move |n| *n = args.0)
}
}

View file

@ -21,7 +21,7 @@ fn effect_runs() {
assert_eq!(b.borrow().as_str(), "Value is -1");
set_a(|a| *a = 1);
set_a(1);
assert_eq!(b.borrow().as_str(), "Value is 1");
})
@ -50,7 +50,7 @@ fn effect_tracks_memo() {
assert_eq!(b().as_str(), "Value is -1");
assert_eq!(c.borrow().as_str(), "Value is -1");
set_a(|a| *a = 1);
set_a(1);
assert_eq!(b().as_str(), "Value is 1");
assert_eq!(c.borrow().as_str(), "Value is 1");
@ -80,7 +80,7 @@ fn untrack_mutes_effect() {
assert_eq!(a(), -1);
assert_eq!(b.borrow().as_str(), "Value is -1");
set_a(|a| *a = 1);
set_a(1);
assert_eq!(a(), 1);
assert_eq!(b.borrow().as_str(), "Value is -1");

View file

@ -16,9 +16,9 @@ fn memo_with_computed_value() {
let (b, set_b) = create_signal(cx, 0);
let c = create_memo(cx, move |_| a() + b());
assert_eq!(c(), 0);
set_a(|a| *a = 5);
set_a(5);
assert_eq!(c(), 5);
set_b(|b| *b = 1);
set_b(1);
assert_eq!(c(), 6);
})
.dispose()
@ -33,11 +33,11 @@ fn nested_memos() {
let d = create_memo(cx, move |_| c() * 2);
let e = create_memo(cx, move |_| d() + 1);
assert_eq!(d(), 0);
set_a(|a| *a = 5);
set_a(5);
assert_eq!(c(), 5);
assert_eq!(d(), 10);
assert_eq!(e(), 11);
set_b(|b| *b = 1);
set_b(1);
assert_eq!(c(), 6);
assert_eq!(d(), 12);
assert_eq!(e(), 13);
@ -79,7 +79,7 @@ fn memo_runs_only_when_inputs_change() {
assert_eq!(call_count.get(), 1);
// and we only call it again when an input changes
set_a(|n| *n = 1);
set_a(1);
assert_eq!(c(), 1);
assert_eq!(call_count.get(), 2);
})

View file

@ -5,7 +5,7 @@ fn basic_signal() {
create_scope(|cx| {
let (a, set_a) = create_signal(cx, 0);
assert_eq!(a(), 0);
set_a(|a| *a = 5);
set_a(5);
assert_eq!(a(), 5);
})
.dispose()
@ -18,9 +18,9 @@ fn derived_signals() {
let (b, set_b) = create_signal(cx, 0);
let c = move || a() + b();
assert_eq!(c(), 0);
set_a(|a| *a = 5);
set_a(5);
assert_eq!(c(), 5);
set_b(|b| *b = 1);
set_b(1);
assert_eq!(c(), 6);
})
.dispose()