Improved naming conventions and remove an extra clone in create_signal_owned

This commit is contained in:
Greg Johnston 2022-07-31 19:40:55 -04:00
parent 17c2faeb56
commit b21973783d
7 changed files with 34 additions and 34 deletions

View File

@ -19,7 +19,7 @@ struct CounterUpdater {
#[component]
fn Parent(cx: Scope) -> web_sys::Element {
let (value, _) = cx.signal(0);
let (value, _) = cx.create_signal(0);
view! {
<div>

View File

@ -1,7 +1,7 @@
use leptos::*;
pub fn simple_counter(cx: Scope) -> web_sys::Element {
let (value, set_value) = cx.signal(0);
let (value, set_value) = cx.create_signal(0);
view! {
<div>

View File

@ -9,15 +9,15 @@ struct CounterUpdater {
#[component]
pub fn Counters(cx: Scope) -> web_sys::Element {
let (next_counter_id, set_next_counter_id) = cx.signal(0);
let (counters, set_counters) = cx.signal::<CounterHolder>(Vec::new());
let (next_counter_id, set_next_counter_id) = cx.create_signal(0);
let (counters, set_counters) = cx.create_signal::<CounterHolder>(Vec::new());
cx.provide_context(CounterUpdater {
set_counters: (*set_counters).clone(),
});
let add_counter = move |_| {
let id = *next_counter_id.get_untracked();
let (read, write) = cx.signal(0);
let (read, write) = cx.create_signal(0);
set_counters(|counters| counters.push((id, (read.clone(), write.clone()))));
set_next_counter_id(|id| *id += 1);
};
@ -25,7 +25,7 @@ pub fn Counters(cx: Scope) -> web_sys::Element {
let add_many_counters = move |_| {
let mut new_counters = vec![];
for next_id in 0..1000 {
let signal = cx.signal(0);
let signal = cx.create_signal(0);
new_counters.push((next_id, (signal.0.clone(), signal.1.clone())));
}
set_counters(move |n| *n = new_counters.clone());

View File

@ -30,7 +30,7 @@ impl Todos {
} else {
Vec::new()
};
let (todos, set_todos) = cx.signal_cloned(starting_todos);
let (todos, set_todos) = cx.create_signal_owned(starting_todos);
Self { todos, set_todos }
}
@ -99,8 +99,8 @@ impl Todo {
}
pub fn new_with_completed(cx: Scope, id: usize, title: String, completed: bool) -> Self {
let (title, set_title) = cx.signal_cloned(title);
let (completed, set_completed) = cx.signal_cloned(completed);
let (title, set_title) = cx.create_signal_owned(title);
let (completed, set_completed) = cx.create_signal_owned(completed);
Self {
id,
title,
@ -128,7 +128,7 @@ pub fn TodoMVC(cx: Scope) -> Vec<Element> {
cx.provide_context(todos.clone());
let todos = cx.create_ref(todos);
let (mode, set_mode) = cx.signal(Mode::All);
let (mode, set_mode) = cx.create_signal(Mode::All);
window_event_listener("hashchange", move |_| {
let new_mode = location_hash().map(|hash| route(&hash)).unwrap_or_default();
set_mode(|mode| *mode = new_mode);
@ -150,7 +150,7 @@ pub fn TodoMVC(cx: Scope) -> Vec<Element> {
}
};
let filtered_todos = cx.memo::<Vec<Todo>>(move || {
let filtered_todos = cx.create_memo::<Vec<Todo>>(move || {
let todos = todos.todos.get();
match *mode.get() {
Mode::All => todos.iter().cloned().collect(),
@ -239,7 +239,7 @@ pub fn Todo(cx: Scope, todo: Todo) -> Element {
// creates a scope-bound reference to the Todo
// this allows us to move the reference into closures below without cloning it
let todo = cx.create_ref(todo);
let (editing, set_editing) = cx.signal(false);
let (editing, set_editing) = cx.create_signal(false);
let todos = cx.use_context::<Todos>().unwrap();
let input: web_sys::Element;

View File

@ -31,7 +31,7 @@ where
let mut mapped: Vec<U> = Vec::new();
let mut disposers: Vec<Option<ScopeDisposer<'a>>> = Vec::new();
let (item_signal, set_item_signal) = cx.signal(Vec::new());
let (item_signal, set_item_signal) = cx.create_signal(Vec::new());
// Diff and update signal each time list is updated.
cx.create_effect(move || {

View File

@ -23,10 +23,10 @@ mod tests {
fn compute_signal() {
let stack = Box::leak(Box::new(RootContext::new()));
let _ = create_scope(stack, |cx| {
let (a, set_a) = cx.signal(0);
let (b, set_b) = cx.signal(0);
let (a, set_a) = cx.create_signal(0);
let (b, set_b) = cx.create_signal(0);
let c = cx.memo(|| *(&a)() + *(&b)());
let c = cx.create_memo(|| *(&a)() + *(&b)());
assert_eq!(*c.get_untracked(), 0);
@ -44,11 +44,11 @@ mod tests {
fn memo_with_conditional_branches() {
let stack = Box::leak(Box::new(RootContext::new()));
let _ = create_scope(stack, |cx| {
let (first_name, set_first_name) = cx.signal("Greg");
let (last_name, set_last_name) = cx.signal("Johnston");
let (show_last_name, set_show_last_name) = cx.signal(true);
let (first_name, set_first_name) = cx.create_signal("Greg");
let (last_name, set_last_name) = cx.create_signal("Johnston");
let (show_last_name, set_show_last_name) = cx.create_signal(true);
let out = cx.memo(move || {
let out = cx.create_memo(move || {
if *(&show_last_name)() {
format!("{} {}", *(&first_name)(), *(&last_name)())
} else {

View File

@ -52,13 +52,13 @@ impl<'a, 'b> BoundedScope<'a, 'b> {
self.inner.create_ref(value)
}
pub fn signal<T>(self, value: T) -> (&'a ReadSignal<T>, &'a WriteSignal<T>) {
self.inner.signal(value)
pub fn create_signal<T>(self, value: T) -> (&'a ReadSignal<T>, &'a WriteSignal<T>) {
let (read, write) = self.inner.signal(value);
(self.create_ref(read), self.create_ref(write))
}
pub fn signal_cloned<T>(self, value: T) -> (ReadSignal<T>, WriteSignal<T>) {
let (read, write) = self.inner.signal(value);
(read.clone(), write.clone())
pub fn create_signal_owned<T>(self, value: T) -> (ReadSignal<T>, WriteSignal<T>) {
self.inner.signal(value)
}
/// An effect is an observer that runs a side effect that depends Signals.
@ -72,8 +72,8 @@ impl<'a, 'b> BoundedScope<'a, 'b> {
self.inner.root_context.untrack(f)
}
pub fn memo<T>(self, f: impl Fn() -> T) -> &'a ReadSignal<T> {
self.inner.memo(f)
pub fn create_memo<T>(self, f: impl Fn() -> T) -> &'a ReadSignal<T> {
self.inner.create_memo(f)
}
pub fn provide_context<T: 'static>(self, value: T) {
@ -149,20 +149,20 @@ impl<'a> ScopeInner<'a> {
self.arena.alloc(value)
}
pub fn signal<T>(&self, value: T) -> (&ReadSignal<T>, &WriteSignal<T>) {
pub fn signal<T>(&self, value: T) -> (ReadSignal<T>, WriteSignal<T>) {
let state = Rc::new(SignalState {
value: RefCell::new(value),
subscriptions: RefCell::new(HashSet::new()),
});
let writer = self.create_ref(WriteSignal {
let writer = WriteSignal {
inner: Rc::downgrade(&state),
});
};
let reader = self.create_ref(ReadSignal {
let reader = ReadSignal {
stack: self.root_context,
inner: state,
});
};
(reader, writer)
}
@ -171,7 +171,7 @@ impl<'a> ScopeInner<'a> {
self.root_context.untrack(f)
}
pub fn memo<T>(&self, f: impl Fn() -> T) -> &ReadSignal<T> {
pub fn create_memo<T>(&self, f: impl Fn() -> T) -> &ReadSignal<T> {
// the initial value should simply be an untracked call, based on initial Signal values
// we need this initial call because the Signal must always have a value
// (otherwise every computed Signal would be typed Signal<Option<T>>)
@ -185,7 +185,7 @@ impl<'a> ScopeInner<'a> {
// and start tracking based on whatever Signals are inside the computed fn
self.create_effect(move || write(|n| *n = f()));
read
self.create_ref(read)
}
/// An effect is an observer that runs a side effect that depends Signals.