Skip to content

Commit 7c9ead2

Browse files
voidpumpkinsiku2
authored andcommitted
update to yew master
1 parent 5d16fe5 commit 7c9ead2

18 files changed

+1065
-923
lines changed

frameworks/keyed/yew-baseline/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ crate-type = ["cdylib"]
1010
[dependencies]
1111
rand = { version = "0.7.3", features = ["wasm-bindgen", "small_rng"] }
1212
wasm-bindgen = "0.2.62"
13+
web-sys = { version = "0.3.55", features = ["Window"]}
1314

1415
# DO NOT MODIFY
1516
yew = { git = "https://github.com/yewstack/yew", branch = "master" }

frameworks/keyed/yew-baseline/bundled-dist/js-framework-benchmark-yew.js

Lines changed: 231 additions & 152 deletions
Large diffs are not rendered by default.
Binary file not shown.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
use rand::prelude::*;
2+
use std::cmp::min;
3+
use yew::prelude::*;
4+
5+
use crate::jumbotron::Jumbotron;
6+
use crate::row::Row;
7+
use crate::row_data::RowData;
8+
9+
pub struct App {
10+
rows: Vec<RowData>,
11+
next_id: usize,
12+
selected_id: Option<usize>,
13+
rng: SmallRng,
14+
on_select: Callback<usize>,
15+
on_remove: Callback<usize>,
16+
}
17+
18+
pub enum Msg {
19+
Run(usize),
20+
Add(usize),
21+
Update(usize),
22+
Clear,
23+
Swap,
24+
Remove(usize),
25+
Select(usize),
26+
}
27+
28+
impl Component for App {
29+
type Message = Msg;
30+
type Properties = ();
31+
32+
fn create(ctx: &Context<Self>) -> Self {
33+
App {
34+
rows: Vec::new(),
35+
next_id: 1,
36+
selected_id: None,
37+
rng: SmallRng::from_entropy(),
38+
on_select: ctx.link().callback(Msg::Select),
39+
on_remove: ctx.link().callback(Msg::Remove),
40+
}
41+
}
42+
43+
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
44+
match msg {
45+
Msg::Run(amount) => {
46+
let rng = &mut self.rng;
47+
let next_id = self.next_id;
48+
let update_amount = min(amount, self.rows.len());
49+
for index in 0..update_amount {
50+
self.rows[index] = RowData::new(next_id + index, rng);
51+
}
52+
self.rows.extend(
53+
(update_amount..amount).map(|index| RowData::new(next_id + index, rng)),
54+
);
55+
self.next_id += amount;
56+
}
57+
Msg::Add(amount) => {
58+
let rng = &mut self.rng;
59+
let next_id = self.next_id;
60+
self.rows
61+
.extend((0..amount).map(|index| RowData::new(next_id + index, rng)));
62+
self.next_id += amount;
63+
}
64+
Msg::Update(step) => {
65+
for index in (0..self.rows.len()).step_by(step) {
66+
self.rows[index].label += " !!!";
67+
}
68+
}
69+
Msg::Clear => {
70+
self.rows.clear();
71+
}
72+
Msg::Swap => {
73+
if self.rows.len() > 998 {
74+
self.rows.swap(1, 998);
75+
}
76+
}
77+
Msg::Remove(id) => {
78+
if let Some(index) = self.rows.iter().position(|row| row.id == id) {
79+
self.rows.remove(index);
80+
}
81+
}
82+
Msg::Select(id) => {
83+
self.selected_id = Some(id);
84+
}
85+
}
86+
true
87+
}
88+
89+
fn view(&self, ctx: &Context<Self>) -> Html {
90+
let rows: Html = self
91+
.rows
92+
.iter()
93+
.map(|row| {
94+
html! {
95+
<Row
96+
key={row.id}
97+
data={row.clone()}
98+
selected={self.selected_id == Some(row.id)}
99+
on_select={self.on_select.clone()}
100+
on_remove={self.on_remove.clone()}
101+
/>
102+
}
103+
})
104+
.collect();
105+
106+
html! {
107+
<div class="container">
108+
<Jumbotron
109+
on_run={ctx.link().callback(Msg::Run)}
110+
on_add={ctx.link().callback(Msg::Add)}
111+
on_update={ctx.link().callback(Msg::Update)}
112+
on_clear={ctx.link().callback(|_| Msg::Clear)}
113+
on_swap={ctx.link().callback(|_| Msg::Swap)}
114+
/>
115+
<table class="table table-hover table-striped test-data">
116+
<tbody id="tbody">
117+
{ rows }
118+
</tbody>
119+
</table>
120+
<span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span>
121+
</div>
122+
}
123+
}
124+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
pub static ADJECTIVES: &[&str] = &[
2+
"pretty",
3+
"large",
4+
"big",
5+
"small",
6+
"tall",
7+
"short",
8+
"long",
9+
"handsome",
10+
"plain",
11+
"quaint",
12+
"clean",
13+
"elegant",
14+
"easy",
15+
"angry",
16+
"crazy",
17+
"helpful",
18+
"mushy",
19+
"odd",
20+
"unsightly",
21+
"adorable",
22+
"important",
23+
"inexpensive",
24+
"cheap",
25+
"expensive",
26+
"fancy",
27+
];
28+
29+
pub static COLOURS: &[&str] = &[
30+
"red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
31+
"orange",
32+
];
33+
34+
pub static NOUNS: &[&str] = &[
35+
"table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
36+
"pizza", "mouse", "keyboard",
37+
];
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use yew::prelude::*;
2+
3+
#[derive(Properties, Clone, PartialEq)]
4+
pub struct Props {
5+
pub on_run: Callback<usize>,
6+
pub on_add: Callback<usize>,
7+
pub on_update: Callback<usize>,
8+
pub on_clear: Callback<()>,
9+
pub on_swap: Callback<()>,
10+
}
11+
12+
pub struct Jumbotron {}
13+
14+
impl Component for Jumbotron {
15+
type Properties = Props;
16+
type Message = ();
17+
18+
fn create(_ctx: &Context<Self>) -> Self {
19+
Self {}
20+
}
21+
22+
fn view(&self, ctx: &Context<Self>) -> Html {
23+
html! {
24+
<div class="jumbotron">
25+
<div class="row">
26+
<div class="col-md-6">
27+
<h1>{ "Yew" }</h1>
28+
</div>
29+
<div class="col-md-6">
30+
<div class="row">
31+
<div class="col-sm-6 smallpad">
32+
<button type="button" id="run" class="btn btn-primary btn-block" onclick={ctx.props().on_run.reform(|_| 1_000)}>{ "Create 1,000 rows" }</button>
33+
</div>
34+
<div class="col-sm-6 smallpad">
35+
<button type="button" class="btn btn-primary btn-block" onclick={ctx.props().on_run.reform(|_| 10_000)} id="runlots">{ "Create 10,000 rows" }</button>
36+
</div>
37+
<div class="col-sm-6 smallpad">
38+
<button type="button" class="btn btn-primary btn-block" onclick={ctx.props().on_add.reform(|_| 1_000)} id="add">{ "Append 1,000 rows" }</button>
39+
</div>
40+
<div class="col-sm-6 smallpad">
41+
<button type="button" class="btn btn-primary btn-block" onclick={ctx.props().on_update.reform(|_| 10)} id="update">{ "Update every 10th row" }</button>
42+
</div>
43+
<div class="col-sm-6 smallpad">
44+
<button type="button" class="btn btn-primary btn-block" onclick={ctx.props().on_clear.reform(|_| ())} id="clear">{ "Clear" }</button>
45+
</div>
46+
<div class="col-sm-6 smallpad">
47+
<button type="button" class="btn btn-primary btn-block" onclick={ctx.props().on_swap.reform(|_| ())} id="swaprows">{ "Swap Rows" }</button>
48+
</div>
49+
</div>
50+
</div>
51+
</div>
52+
</div>
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)