Skip to content

Commit f2f52b2

Browse files
authored
change: move signal method implementations into traits in signal prelude (leptos-rs#490)
1 parent 46d6e3f commit f2f52b2

File tree

28 files changed

+1919
-1083
lines changed

28 files changed

+1919
-1083
lines changed

examples/hackernews_axum/src/error_template.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
use leptos::{view, Errors, For, ForProps, IntoView, RwSignal, Scope, View};
1+
use leptos::{
2+
signal_prelude::*, view, Errors, For, ForProps, IntoView, RwSignal, Scope,
3+
View,
4+
};
25

36
// A basic function to display errors served by the error boundaries. Feel free to do more complicated things
47
// here than just displaying them
58
pub fn error_template(cx: Scope, errors: Option<RwSignal<Errors>>) -> View {
69
let Some(errors) = errors else {
710
panic!("No Errors found and we expected errors!");
811
};
12+
913
view! {cx,
10-
<h1>"Errors"</h1>
11-
<For
12-
// a function that returns the items we're iterating over; a signal is fine
13-
each=errors
14-
// a unique key for each item as a reference
15-
key=|(key, _)| key.clone()
16-
// renders each item to a view
17-
view= move |cx, (_, error)| {
18-
let error_string = error.to_string();
19-
view! {
20-
cx,
21-
<p>"Error: " {error_string}</p>
14+
<h1>"Errors"</h1>
15+
<For
16+
// a function that returns the items we're iterating over; a signal is fine
17+
each=errors
18+
// a unique key for each item as a reference
19+
key=|(key, _)| key.clone()
20+
// renders each item to a view
21+
view= move |cx, (_, error)| {
22+
let error_string = error.to_string();
23+
view! {
24+
cx,
25+
<p>"Error: " {error_string}</p>
26+
}
2227
}
23-
}
2428
/>
2529
}
2630
.into_view(cx)

examples/todomvc/src/storage.rs

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
use crate::Todo;
2-
use leptos::Scope;
3-
use serde::{Deserialize, Serialize};
2+
use leptos::{
3+
signal_prelude::*,
4+
Scope,
5+
};
6+
use serde::{
7+
Deserialize,
8+
Serialize,
9+
};
410
use uuid::Uuid;
511

612
#[derive(Serialize, Deserialize)]
713
pub struct TodoSerialized {
8-
pub id: Uuid,
9-
pub title: String,
10-
pub completed: bool,
14+
pub id: Uuid,
15+
pub title: String,
16+
pub completed: bool,
1117
}
1218

1319
impl TodoSerialized {
14-
pub fn into_todo(self, cx: Scope) -> Todo {
15-
Todo::new_with_completed(cx, self.id, self.title, self.completed)
16-
}
20+
pub fn into_todo(self, cx: Scope) -> Todo {
21+
Todo::new_with_completed(cx, self.id, self.title, self.completed)
22+
}
1723
}
1824

1925
impl From<&Todo> for TodoSerialized {
20-
fn from(todo: &Todo) -> Self {
21-
Self {
22-
id: todo.id,
23-
title: todo.title.get(),
24-
completed: todo.completed.get(),
25-
}
26+
fn from(todo: &Todo) -> Self {
27+
Self {
28+
id: todo.id,
29+
title: todo.title.get(),
30+
completed: todo.completed.get(),
2631
}
32+
}
2733
}

integrations/actix/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ pub fn handle_server_fns_with_context(
282282
/// The provides a [MetaContext] and a [RouterIntegrationContext] to app’s context before
283283
/// rendering it, and includes any meta tags injected using [leptos_meta].
284284
///
285-
/// The HTML stream is rendered using [render_to_stream], and includes everything described in
286-
/// the documentation for that function.
285+
/// The HTML stream is rendered using [render_to_stream](leptos::ssr::render_to_stream), and
286+
/// includes everything described in the documentation for that function.
287287
///
288288
/// This can then be set up at an appropriate route in your application:
289289
/// ```
@@ -602,8 +602,8 @@ where
602602
/// The provides a [MetaContext] and a [RouterIntegrationContext] to app’s context before
603603
/// rendering it, and includes any meta tags injected using [leptos_meta].
604604
///
605-
/// The HTML stream is rendered using [render_to_stream], and includes everything described in
606-
/// the documentation for that function.
605+
/// The HTML stream is rendered using [render_to_stream](leptos::ssr::render_to_stream), and
606+
/// includes everything described in the documentation for that function.
607607
///
608608
/// This can then be set up at an appropriate route in your application:
609609
/// ```

integrations/axum/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ pub type PinnedHtmlStream =
342342
/// The provides a [MetaContext] and a [RouterIntegrationContext] to app’s context before
343343
/// rendering it, and includes any meta tags injected using [leptos_meta].
344344
///
345-
/// The HTML stream is rendered using [render_to_stream], and includes everything described in
346-
/// the documentation for that function.
345+
/// The HTML stream is rendered using [render_to_stream](leptos::ssr::render_to_stream), and
346+
/// includes everything described in the documentation for that function.
347347
///
348348
/// This can then be set up at an appropriate route in your application:
349349
/// ```

leptos/src/error_boundary.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::Children;
22
use leptos_dom::{Errors, IntoView};
33
use leptos_macro::{component, view};
4-
use leptos_reactive::{create_rw_signal, provide_context, RwSignal, Scope};
4+
use leptos_reactive::{
5+
create_rw_signal, provide_context, signal_prelude::*, RwSignal, Scope,
6+
};
57

68
/// When you render a `Result<_, _>` in your view, in the `Err` case it will
79
/// render nothing, and search up through the view tree for an `<ErrorBoundary/>`.

leptos/src/show.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use leptos::component;
22
use leptos_dom::{Fragment, IntoView};
3-
use leptos_reactive::{create_memo, Scope};
3+
use leptos_reactive::{create_memo, signal_prelude::*, Scope};
44

55
/// A component that will show its children when the `when` condition is `true`,
66
/// and show the fallback when it is `false`, without rerendering every time

leptos/src/suspense.rs

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ where
8383
fallback().into_view(cx)
8484
}
8585
} else {
86+
use leptos_reactive::signal_prelude::*;
87+
8688
// run the child; we'll probably throw this away, but it will register resource reads
8789
let child = orig_child(cx).into_view(cx);
8890
let after_original_child = HydrationCtx::id();

leptos/tests/ssr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ fn ssr_with_styles() {
147147
use leptos::*;
148148

149149
_ = create_scope(create_runtime(), |cx| {
150-
let (value, set_value) = create_signal(cx, 0);
150+
let (_, set_value) = create_signal(cx, 0);
151151
let styles = "myclass";
152152
let rendered = view! {
153153
cx, class = styles,
@@ -170,7 +170,7 @@ fn ssr_option() {
170170
use leptos::*;
171171

172172
_ = create_scope(create_runtime(), |cx| {
173-
let (value, set_value) = create_signal(cx, 0);
173+
let (_, _) = create_signal(cx, 0);
174174
let rendered = view! {
175175
cx,
176176
<option/>

leptos_dom/src/components/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{HydrationCtx, IntoView};
22
use cfg_if::cfg_if;
3-
use leptos_reactive::{use_context, RwSignal};
3+
use leptos_reactive::{signal_prelude::*, use_context, RwSignal};
44
use std::{collections::HashMap, error::Error, sync::Arc};
55

66
/// A struct to hold all the possible errors that could be provided by child Views

leptos_dom/src/node_ref.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::{html::ElementDescriptor, HtmlElement};
2-
use leptos_reactive::{create_effect, create_rw_signal, RwSignal, Scope};
2+
use leptos_reactive::{
3+
create_effect, create_rw_signal, signal_prelude::*, RwSignal, Scope,
4+
};
35
use std::cell::Cell;
46

57
/// Contains a shared reference to a DOM node created while using the `view`

leptos_macro/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ pub fn view(tokens: TokenStream) -> TokenStream {
347347
}
348348

349349
/// An optimized, cached template for client-side rendering. Follows the same
350-
/// syntax as the [view](crate::macro) macro. In hydration or server-side rendering mode,
350+
/// syntax as the [view!] macro. In hydration or server-side rendering mode,
351351
/// behaves exactly as the `view` macro. In client-side rendering mode, uses a `<template>`
352352
/// node to efficiently render the element. Should only be used with a single root element.
353353
#[proc_macro_error::proc_macro_error]

leptos_reactive/src/lib.rs

+3-39
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
#[cfg_attr(debug_assertions, macro_use)]
7070
extern crate tracing;
7171

72+
#[macro_use]
73+
mod signal;
7274
mod context;
7375
mod effect;
7476
mod hydration;
@@ -78,7 +80,6 @@ mod runtime;
7880
mod scope;
7981
mod selector;
8082
mod serialization;
81-
mod signal;
8283
mod signal_wrappers_read;
8384
mod signal_wrappers_write;
8485
mod slice;
@@ -96,7 +97,7 @@ pub use runtime::{create_runtime, RuntimeId};
9697
pub use scope::*;
9798
pub use selector::*;
9899
pub use serialization::*;
99-
pub use signal::*;
100+
pub use signal::{prelude as signal_prelude, *};
100101
pub use signal_wrappers_read::*;
101102
pub use signal_wrappers_write::*;
102103
pub use slice::*;
@@ -105,43 +106,6 @@ pub use spawn_microtask::*;
105106
pub use stored_value::*;
106107
pub use suspense::SuspenseContext;
107108

108-
/// Trait implemented for all signal types which you can `get` a value
109-
/// from, such as [`ReadSignal`],
110-
/// [`Memo`], etc., which allows getting the inner value without
111-
/// subscribing to the current scope.
112-
pub trait UntrackedGettableSignal<T> {
113-
/// Gets the signal's value without creating a dependency on the
114-
/// current scope.
115-
fn get_untracked(&self) -> T
116-
where
117-
T: Clone;
118-
119-
/// Runs the provided closure with a reference to the current
120-
/// value without creating a dependency on the current scope.
121-
fn with_untracked<O>(&self, f: impl FnOnce(&T) -> O) -> O;
122-
}
123-
124-
/// Trait implemented for all signal types which you can `set` the inner
125-
/// value, such as [`WriteSignal`] and [`RwSignal`], which allows setting
126-
/// the inner value without causing effects which depend on the signal
127-
/// from being run.
128-
pub trait UntrackedSettableSignal<T> {
129-
/// Sets the signal's value without notifying dependents.
130-
fn set_untracked(&self, new_value: T);
131-
132-
/// Runs the provided closure with a mutable reference to the current
133-
/// value without notifying dependents.
134-
fn update_untracked(&self, f: impl FnOnce(&mut T));
135-
136-
/// Runs the provided closure with a mutable reference to the current
137-
/// value without notifying dependents and returns
138-
/// the value the closure returned.
139-
fn update_returning_untracked<U>(
140-
&self,
141-
f: impl FnOnce(&mut T) -> U,
142-
) -> Option<U>;
143-
}
144-
145109
mod macros {
146110
macro_rules! debug_warn {
147111
($($x:tt)*) => {

0 commit comments

Comments
 (0)