forked from leptos-rs/leptos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improved
<For/>
algorithm (leptos-rs#1146)
Rewrites the algorithm behind the `<For/>` component to create a more robust keyed list implementation, with the potential for future additional optimizations related to grouping moved ranges. Closes leptos-rs#533.
- Loading branch information
1 parent
fea1377
commit f99f2df
Showing
6 changed files
with
938 additions
and
335 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[build] | ||
# [build] | ||
# target = "wasm32-unknown-unknown" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,142 @@ | ||
#![feature(iter_intersperse)] | ||
#![allow(warnings)] | ||
|
||
#[macro_use] | ||
extern crate tracing; | ||
|
||
mod utils; | ||
|
||
use leptos::*; | ||
use tracing::field::debug; | ||
use tracing_subscriber::util::SubscriberInitExt; | ||
|
||
fn main() { | ||
console_error_panic_hook::set_once(); | ||
|
||
tracing_subscriber::fmt() | ||
.with_max_level(tracing::Level::TRACE) | ||
.without_time() | ||
.with_file(true) | ||
.with_line_number(true) | ||
.with_target(false) | ||
.with_writer(utils::MakeConsoleWriter) | ||
.with_ansi(false) | ||
.pretty() | ||
.finish() | ||
.init(); | ||
|
||
mount_to_body(view_fn); | ||
console_error_panic_hook::set_once(); | ||
|
||
tracing_subscriber::fmt() | ||
.with_max_level(tracing::Level::TRACE) | ||
.without_time() | ||
.with_file(true) | ||
.with_line_number(true) | ||
.with_target(false) | ||
.with_writer(tracing_subscriber_wasm::MakeConsoleWriter::default()) | ||
.with_ansi(false) | ||
.pretty() | ||
.finish() | ||
.init(); | ||
|
||
mount_to_body(view_fn); | ||
} | ||
|
||
fn view_fn(cx: Scope) -> impl IntoView { | ||
let view = view! { cx, | ||
<For | ||
each=|| vec![0, 1, 2, 3, 4, 5, 6, 7] | ||
key=|i| *i | ||
view=|cx, i| view! { cx, {i} } | ||
/> | ||
} | ||
.into_view(cx); | ||
|
||
let (a, set_a) = create_signal(cx, view.clone()); | ||
let (b, set_b) = create_signal(cx, view); | ||
// fn view_fn(cx: Scope) -> impl IntoView { | ||
// view! { cx, | ||
// <h2>"Passing Tests"</h2> | ||
// <ul> | ||
// /* These work! */ | ||
// <Test from=[1] to=[] /> | ||
// <Test from=[1, 2] to=[] /> | ||
// <Test from=[1, 2, 3] to=[] /> | ||
// <hr/> | ||
// <Test from=[] to=[1] /> | ||
// <Test from=[1, 2] to=[1] /> | ||
// <Test from=[2, 1] to=[1] /> | ||
// <hr/> | ||
// <Test from=[1, 2, 3] to=[1, 2] /> | ||
// <Test from=[2] to=[1, 2] /> | ||
// <Test from=[1] to=[1, 2] /> | ||
// <Test from=[] to=[1, 2, 3] /> | ||
// <Test from=[2] to=[1, 2, 3] /> | ||
// <Test from=[1] to=[1, 2, 3] /> | ||
// <Test from=[1, 3, 2] to=[1, 2, 3] /> | ||
// <Test from=[2, 1, 3] to=[1, 2, 3] /> | ||
// </ul> | ||
// <h2>"Broken Tests"</h2> | ||
// <ul> | ||
// <Test from=[3] to=[1, 2, 3] /> | ||
// <Test from=[3, 1] to=[1, 2, 3] /> | ||
// <Test from=[3, 2, 1] to=[1, 2, 3] /> | ||
// <hr/> | ||
// <Test from=[1, 4, 2, 3] to=[1, 2, 3, 4] /> | ||
// <hr/> | ||
// <Test from=[1, 4, 3, 2, 5] to=[1, 2, 3, 4, 5] /> | ||
// <Test from=[4, 5, 3, 1, 2] to=[1, 2, 3, 4, 5] /> | ||
// </ul> | ||
// } | ||
// } | ||
|
||
// #[component] | ||
// fn Test<From, To>(cx: Scope, from: From, to: To) -> impl IntoView | ||
// where | ||
// From: IntoIterator<Item = usize>, | ||
// To: IntoIterator<Item = usize>, | ||
// { | ||
// let from = from.into_iter().collect::<Vec<_>>(); | ||
// let to = to.into_iter().collect::<Vec<_>>(); | ||
|
||
// let (list, set_list) = create_signal(cx, from.clone()); | ||
// request_animation_frame({ | ||
// let to = to.clone(); | ||
// move || { | ||
// set_list(to); | ||
// } | ||
// }); | ||
|
||
// view! { cx, | ||
// <li> | ||
// "from: [" {move || | ||
// from | ||
// .iter() | ||
// .map(ToString::to_string) | ||
// .intersperse(", ".to_string()) | ||
// .collect::<String>() | ||
// } "]" | ||
// <br /> | ||
// "to: [" {move || | ||
// to | ||
// .iter() | ||
// .map(ToString::to_string) | ||
// .intersperse(", ".to_string()) | ||
// .collect::<String>() | ||
// } "]" | ||
// <br /> | ||
// "result: [" | ||
// <For | ||
// each=list | ||
// key=|i| *i | ||
// view=|cx, i| { | ||
// view! { cx, <span>{i} ", "</span> } | ||
// } | ||
// /> "]" | ||
// /* <p> | ||
// "Pre | " | ||
// <For | ||
// each=list | ||
// key=|i| *i | ||
// view=|cx, i| { | ||
// view! { cx, <span>{i}</span> } | ||
// } | ||
// /> | ||
// " | Post" | ||
// </p> */ | ||
// </li> | ||
// } | ||
// } | ||
|
||
let (is_a, set_is_a) = create_signal(cx, true); | ||
|
||
let handle_toggle = move |_| { | ||
trace!("toggling"); | ||
if is_a() { | ||
set_b(a()); | ||
|
||
set_is_a(false); | ||
} else { | ||
set_a(a()); | ||
|
||
set_is_a(true); | ||
fn view_fn(cx: Scope) -> impl IntoView { | ||
let (should_show_a, sett_should_show_a) = create_signal(cx, true); | ||
|
||
let a = vec![1, 2, 3, 4]; | ||
let b = vec![1, 2, 3]; | ||
|
||
view! { cx, | ||
<button on:click=move |_| sett_should_show_a.update(|show| *show = !*show)>"Toggle"</button> | ||
|
||
<For | ||
each={move || if should_show_a.get() { | ||
a.clone() | ||
} else { | ||
b.clone() | ||
}} | ||
key=|i| *i | ||
view=|cx, i| view! { cx, <h1>{i}</h1> } | ||
/> | ||
} | ||
}; | ||
|
||
let a_tag = view! { cx, <svg::a/> }; | ||
|
||
view! { cx, | ||
<> | ||
<div> | ||
<button on:click=handle_toggle>"Toggle"</button> | ||
</div> | ||
<svg>{a_tag}</svg> | ||
<Example/> | ||
<A child=Signal::from(a) /> | ||
<A child=Signal::from(b) /> | ||
</> | ||
} | ||
} | ||
|
||
#[component] | ||
fn A(cx: Scope, child: Signal<View>) -> impl IntoView { | ||
move || child() | ||
} | ||
|
||
#[component] | ||
fn Example(cx: Scope) -> impl IntoView { | ||
trace!("rendering <Example/>"); | ||
|
||
let (value, set_value) = create_signal(cx, 10); | ||
|
||
let memo = create_memo(cx, move |_| value() * 2); | ||
let derived = Signal::derive(cx, move || value() * 3); | ||
|
||
create_effect(cx, move |_| { | ||
trace!("logging value of derived..., {}", derived.get()); | ||
}); | ||
|
||
set_timeout( | ||
move || set_value.update(|v| *v += 1), | ||
std::time::Duration::from_millis(50), | ||
); | ||
|
||
view! { cx, | ||
<h1>"Example"</h1> | ||
<button on:click=move |_| set_value.update(|value| *value += 1)> | ||
"Click me" | ||
</button> | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.