Skip to content

Commit a48a299

Browse files
authored
Merge pull request #4255 from leptos-rs/4254
Revert recent broken changes
2 parents 274fe07 + aedcd51 commit a48a299

File tree

5 files changed

+25
-134
lines changed

5 files changed

+25
-134
lines changed

leptos/src/animated_show.rs

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
use crate::{
2-
children::ChildrenFn, component, control_flow::Show, show::IntoCondition,
3-
IntoView,
4-
};
1+
use crate::{children::ChildrenFn, component, control_flow::Show, IntoView};
52
use core::time::Duration;
63
use leptos_dom::helpers::TimeoutHandle;
74
use leptos_macro::view;
85
use reactive_graph::{
9-
diagnostics::SpecialNonReactiveZone,
106
effect::RenderEffect,
117
owner::{on_cleanup, StoredValue},
128
signal::RwSignal,
13-
traits::{GetValue, Set, SetValue},
9+
traits::{Get, GetUntracked, GetValue, Set, SetValue},
10+
wrappers::read::Signal,
1411
};
15-
use std::marker::PhantomData;
1612
use tachys::prelude::*;
1713

1814
/// A component that will show its children when the `when` condition is `true`.
@@ -50,16 +46,14 @@ use tachys::prelude::*;
5046
/// }
5147
/// # }
5248
/// ```
53-
///
54-
/// Please note, that unlike `Show`, `AnimatedShow` does not support a `fallback` prop.
5549
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace", skip_all))]
5650
#[component]
57-
pub fn AnimatedShow<M>(
51+
pub fn AnimatedShow(
5852
/// The components Show wraps
5953
children: ChildrenFn,
60-
/// When true the children are shown.
61-
/// It accepts a closure that returns a boolean value as well as a boolean signal or plain boolean value.
62-
when: impl IntoCondition<M>,
54+
/// If the component should show or not
55+
#[prop(into)]
56+
when: Signal<bool>,
6357
/// Optional CSS class to apply if `when == true`
6458
#[prop(optional)]
6559
show_class: &'static str,
@@ -68,26 +62,17 @@ pub fn AnimatedShow<M>(
6862
hide_class: &'static str,
6963
/// The timeout after which the component will be unmounted if `when == false`
7064
hide_delay: Duration,
71-
72-
/// Marker for generic parameters. Ignore this.
73-
#[prop(optional)]
74-
_marker: PhantomData<M>,
7565
) -> impl IntoView {
76-
let when = when.into_condition();
77-
78-
// Silence warnings about using signals in non-reactive contexts.
79-
#[cfg(debug_assertions)]
80-
let z = SpecialNonReactiveZone::enter();
81-
8266
let handle: StoredValue<Option<TimeoutHandle>> = StoredValue::new(None);
83-
let cls = RwSignal::new(if when.run() { show_class } else { hide_class });
84-
let show = RwSignal::new(when.run());
85-
86-
#[cfg(debug_assertions)]
87-
drop(z);
67+
let cls = RwSignal::new(if when.get_untracked() {
68+
show_class
69+
} else {
70+
hide_class
71+
});
72+
let show = RwSignal::new(when.get_untracked());
8873

8974
let eff = RenderEffect::new(move |_| {
90-
if when.run() {
75+
if when.get() {
9176
// clear any possibly active timer
9277
if let Some(h) = handle.get_value() {
9378
h.clear();
@@ -115,8 +100,8 @@ pub fn AnimatedShow<M>(
115100
});
116101

117102
view! {
118-
<Show when=show>
119-
<div class=cls>{children()}</div>
103+
<Show when=move || show.get() fallback=|| ()>
104+
<div class=move || cls.get()>{children()}</div>
120105
</Show>
121106
}
122107
}

leptos/src/hydration/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ pub fn AutoReload(
2727
None => options.reload_port,
2828
};
2929
let protocol = match options.reload_ws_protocol {
30-
leptos_config::ReloadWSProtocol::Auto => "null",
3130
leptos_config::ReloadWSProtocol::WS => "'ws://'",
3231
leptos_config::ReloadWSProtocol::WSS => "'wss://'",
3332
};

leptos/src/hydration/reload_script.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
let host = window.location.hostname;
2-
3-
if (protocol === null) {
4-
protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
5-
}
6-
72
let ws = new WebSocket(`${protocol}${host}:${reload_port}/live_reload`);
83
ws.onmessage = (ev) => {
94
let msg = JSON.parse(ev.data);

leptos/src/show.rs

Lines changed: 5 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,30 @@
11
use crate::{
22
children::{TypedChildrenFn, ViewFn},
3-
prelude::{FunctionMarker, SignalMarker},
43
IntoView,
54
};
65
use leptos_macro::component;
76
use reactive_graph::{computed::ArcMemo, traits::Get};
8-
use std::{marker::PhantomData, sync::Arc};
97
use tachys::either::Either;
108

11-
/// Shows its children whenever the condition `when` prop is `true`.
12-
/// Otherwise it renders the `fallback` prop, which defaults to the empty view.
13-
///
14-
/// The prop `when` can be a closure that returns a bool, a signal of type bool, or a boolean value.
15-
///
16-
/// ## Usage
17-
///
18-
/// ```
19-
/// # use leptos::prelude::*;
20-
/// #
21-
/// # #[component]
22-
/// # pub fn Demo() -> impl IntoView {
23-
/// let (condition, set_condition) = signal(true);
24-
///
25-
/// view! {
26-
/// <Show when=condition>
27-
/// <p>"Hello, world!"</p>
28-
/// </Show>
29-
/// }
30-
/// # }
31-
/// ```
32-
///
33-
/// Or with a closure as the `when` condition:
34-
///
35-
/// ```
36-
/// # use leptos::prelude::*;
37-
/// #
38-
/// # #[component]
39-
/// # pub fn Demo() -> impl IntoView {
40-
/// let (condition, set_condition) = signal(true);
41-
///
42-
/// view! {
43-
/// <Show when=move || condition.get()>
44-
/// <p>"Hello, world!"</p>
45-
/// </Show>
46-
/// }
47-
/// # }
48-
/// ```
499
#[component]
50-
pub fn Show<M, C>(
10+
pub fn Show<W, C>(
5111
/// The children will be shown whenever the condition in the `when` closure returns `true`.
5212
children: TypedChildrenFn<C>,
53-
/// When true the children are shown, otherwise the fallback.
54-
/// It accepts a closure that returns a boolean value as well as a boolean signal or plain boolean value.
55-
when: impl IntoCondition<M>,
13+
/// A closure that returns a bool that determines whether this thing runs
14+
when: W,
5615
/// A closure that returns what gets rendered if the when statement is false. By default this is the empty view.
5716
#[prop(optional, into)]
5817
fallback: ViewFn,
59-
60-
/// Marker for generic parameters. Ignore this.
61-
#[prop(optional)]
62-
_marker: PhantomData<M>,
6318
) -> impl IntoView
6419
where
20+
W: Fn() -> bool + Send + Sync + 'static,
6521
C: IntoView + 'static,
6622
{
67-
let when = when.into_condition();
68-
let memoized_when = ArcMemo::new(move |_| when.run());
23+
let memoized_when = ArcMemo::new(move |_| when());
6924
let children = children.into_inner();
7025

7126
move || match memoized_when.get() {
7227
true => Either::Left(children()),
7328
false => Either::Right(fallback.run()),
7429
}
7530
}
76-
77-
/// A closure that returns a bool. Can be converted from a closure, a signal, or a boolean value.
78-
pub struct Condition(Arc<dyn Fn() -> bool + Send + Sync + 'static>);
79-
80-
impl Condition {
81-
/// Evaluates the condition and returns its result.
82-
pub fn run(&self) -> bool {
83-
(self.0)()
84-
}
85-
}
86-
87-
/// Trait to convert various types into a `Condition`.
88-
/// Implemented for closures, signals, and boolean values.
89-
pub trait IntoCondition<M> {
90-
/// Does the conversion
91-
fn into_condition(self) -> Condition;
92-
}
93-
94-
impl<S> IntoCondition<SignalMarker> for S
95-
where
96-
S: Get<Value = bool> + Send + Sync + 'static,
97-
{
98-
fn into_condition(self) -> Condition {
99-
Condition(Arc::new(move || self.get()))
100-
}
101-
}
102-
103-
impl<F> IntoCondition<FunctionMarker> for F
104-
where
105-
F: Fn() -> bool + Send + Sync + 'static,
106-
{
107-
fn into_condition(self) -> Condition {
108-
Condition(Arc::new(self))
109-
}
110-
}
111-
112-
impl IntoCondition<Condition> for Condition {
113-
fn into_condition(self) -> Condition {
114-
self
115-
}
116-
}

leptos_config/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl LeptosOptions {
153153
None => None,
154154
},
155155
reload_ws_protocol: ws_from_str(
156-
env_w_default("LEPTOS_RELOAD_WS_PROTOCOL", "auto")?.as_str(),
156+
env_w_default("LEPTOS_RELOAD_WS_PROTOCOL", "ws")?.as_str(),
157157
)?,
158158
not_found_path: env_w_default("LEPTOS_NOT_FOUND_PATH", "/404")?
159159
.into(),
@@ -283,24 +283,22 @@ impl TryFrom<String> for Env {
283283
pub enum ReloadWSProtocol {
284284
WS,
285285
WSS,
286-
Auto,
287286
}
288287

289288
impl Default for ReloadWSProtocol {
290289
fn default() -> Self {
291-
Self::Auto
290+
Self::WS
292291
}
293292
}
294293

295294
fn ws_from_str(input: &str) -> Result<ReloadWSProtocol, LeptosConfigError> {
296295
let sanitized = input.to_lowercase();
297296
match sanitized.as_ref() {
298-
"auto" => Ok(ReloadWSProtocol::Auto),
299297
"ws" | "WS" => Ok(ReloadWSProtocol::WS),
300298
"wss" | "WSS" => Ok(ReloadWSProtocol::WSS),
301299
_ => Err(LeptosConfigError::EnvVarError(format!(
302-
"{input} is not a supported websocket protocol. Use only `auto`, \
303-
`ws` or `wss`.",
300+
"{input} is not a supported websocket protocol. Use only `ws` or \
301+
`wss`.",
304302
))),
305303
}
306304
}

0 commit comments

Comments
 (0)