6
6
//!
7
7
//!
8
8
9
- use std:: rc:: Rc ;
10
-
11
- use dioxus:: events:: on:: MouseEvent ;
12
- use dioxus_core as dioxus;
13
- use dioxus_core:: prelude:: * ;
14
- use dioxus_web:: WebsysRenderer ;
9
+ use dioxus:: { events:: on:: MouseEvent , prelude:: * } ;
15
10
use dioxus_html as dioxus_elements;
16
-
17
-
11
+ use fxhash :: { FxBuildHasher , FxHasher32 } ;
12
+ use std :: rc :: Rc ;
18
13
19
14
fn main ( ) {
20
- wasm_logger:: init ( wasm_logger:: Config :: new ( log:: Level :: Debug ) ) ;
21
- console_error_panic_hook:: set_once ( ) ;
22
15
log:: debug!( "starting!" ) ;
23
- wasm_bindgen_futures :: spawn_local ( WebsysRenderer :: start ( App ) ) ;
16
+ dioxus :: desktop :: launch ( App , |c| c ) ;
24
17
}
25
18
26
19
// We use a special immutable hashmap to make hashmap operations efficient
27
20
type RowList = im_rc:: HashMap < usize , Rc < str > , FxBuildHasher > ;
28
- // type RowList = im_rc::HashMap<usize, Rc<str>, nohash_hasher::BuildNoHashHasher<usize>>;
29
21
30
22
static App : FC < ( ) > = |cx| {
31
- let ( items, set_items) = use_state_classic ( cx, || RowList :: default ( ) ) ;
32
- let ( selection, set_selection) = use_state_classic ( cx, || None as Option < usize > ) ;
23
+ let items = use_state ( cx, || RowList :: default ( ) ) ;
33
24
34
- let create_rendered_rows = move |from, num| move |_| set_items ( create_row_list ( from, num) ) ;
25
+ let create_rendered_rows = move |from, num| move |_| items . set ( create_row_list ( from, num) ) ;
35
26
36
27
let append_1_000_rows =
37
- move |_| set_items ( create_row_list ( items. len ( ) , 1000 ) . union ( items. clone ( ) ) ) ;
28
+ move |_| items . set ( create_row_list ( items. len ( ) , 1000 ) . union ( ( * items) . clone ( ) ) ) ;
38
29
39
30
let update_every_10th_row = move |_| {
40
- let mut new_items = items. clone ( ) ;
31
+ let mut new_items = ( * items) . clone ( ) ;
41
32
let mut small_rng = SmallRng :: from_entropy ( ) ;
42
- new_items
43
- . iter_mut ( )
44
- . step_by ( 10 )
45
- . for_each ( |( _, val) | * val = create_new_row_label ( & mut small_rng) ) ;
46
- set_items ( new_items) ;
33
+ new_items. iter_mut ( ) . step_by ( 10 ) . for_each ( |( _, val) | {
34
+ * val = create_new_row_label ( & mut String :: with_capacity ( 30 ) , & mut small_rng)
35
+ } ) ;
36
+ items. set ( new_items) ;
47
37
} ;
48
- let clear_rows = move |_| set_items ( RowList :: default ( ) ) ;
38
+ let clear_rows = move |_| items . set ( RowList :: default ( ) ) ;
49
39
50
40
let swap_rows = move |_| {
51
41
// this looks a bit ugly because we're using a hashmap instead of a vec
52
42
if items. len ( ) > 998 {
53
- let mut new_items = items. clone ( ) ;
43
+ let mut new_items = ( * items) . clone ( ) ;
54
44
let a = new_items. get ( & 0 ) . unwrap ( ) . clone ( ) ;
55
45
* new_items. get_mut ( & 0 ) . unwrap ( ) = new_items. get ( & 998 ) . unwrap ( ) . clone ( ) ;
56
46
* new_items. get_mut ( & 998 ) . unwrap ( ) = a;
57
- set_items ( new_items) ;
47
+ items . set ( new_items) ;
58
48
}
59
49
} ;
60
50
@@ -83,7 +73,7 @@ static App: FC<()> = |cx| {
83
73
}
84
74
}
85
75
}
86
- table {
76
+ table {
87
77
tbody {
88
78
{ rows}
89
79
}
@@ -93,23 +83,28 @@ static App: FC<()> = |cx| {
93
83
} )
94
84
} ;
95
85
86
+ #[ derive( Clone ) ]
87
+ struct RowController { }
88
+
96
89
#[ derive( Props ) ]
97
90
struct ActionButtonProps < F : Fn ( MouseEvent ) > {
98
91
name : & ' static str ,
99
92
id : & ' static str ,
100
93
action : F ,
101
94
}
102
- fn ActionButton < F : Fn ( MouseEvent ) > ( cx : Context < ActionButtonProps < F > > ) -> VNode {
95
+ fn ActionButton < F > ( cx : Context < ActionButtonProps < F > > ) -> VNode
96
+ where
97
+ F : Fn ( MouseEvent ) ,
98
+ {
103
99
cx. render ( rsx ! {
104
100
div { class: "col-sm-6 smallpad"
105
- button { class: "btn btn-primary btn-block" , r#type: "button" , id: "{cx.id}" , onclick: { & cx. action} ,
101
+ button { class: "btn btn-primary btn-block" , r#type: "button" , id: "{cx.id}" , onclick: { & cx. action} ,
106
102
"{cx.name}"
107
103
}
108
104
}
109
105
} )
110
106
}
111
107
112
-
113
108
#[ derive( PartialEq , Props ) ]
114
109
struct RowProps {
115
110
row_id : usize ,
@@ -132,22 +127,25 @@ fn Row<'a>(cx: Context<'a, RowProps>) -> VNode {
132
127
} )
133
128
}
134
129
135
- use fxhash:: { FxBuildHasher , FxHasher32 } ;
136
130
use rand:: prelude:: * ;
137
- fn create_new_row_label ( rng : & mut SmallRng ) -> Rc < str > {
138
- let mut label = String :: new ( ) ;
131
+ fn create_new_row_label ( label : & mut String , rng : & mut SmallRng ) -> Rc < str > {
139
132
label. push_str ( ADJECTIVES . choose ( rng) . unwrap ( ) ) ;
140
133
label. push ( ' ' ) ;
141
134
label. push_str ( COLOURS . choose ( rng) . unwrap ( ) ) ;
142
135
label. push ( ' ' ) ;
143
136
label. push_str ( NOUNS . choose ( rng) . unwrap ( ) ) ;
144
- Rc :: from ( label)
137
+ Rc :: from ( label. as_ref ( ) )
145
138
}
146
139
147
140
fn create_row_list ( from : usize , num : usize ) -> RowList {
148
141
let mut small_rng = SmallRng :: from_entropy ( ) ;
142
+ let mut buf = String :: with_capacity ( 35 ) ;
149
143
( from..num + from)
150
- . map ( |f| ( f, create_new_row_label ( & mut small_rng) ) )
144
+ . map ( |f| {
145
+ let o = ( f, create_new_row_label ( & mut buf, & mut small_rng) ) ;
146
+ buf. clear ( ) ;
147
+ o
148
+ } )
151
149
. collect :: < RowList > ( )
152
150
}
153
151
0 commit comments