@@ -5,6 +5,7 @@ use std::hash::Hash;
5
5
use std:: ops:: Deref ;
6
6
#[ cfg( client) ]
7
7
use sycamore:: prelude:: Scope ;
8
+ use sycamore:: reactive:: { create_rc_signal, RcSignal } ;
8
9
9
10
/// A reactive version of [`HashMap`] that uses nested reactivity on its
10
11
/// elements. That means the type inside the vector must implement [`MakeRx`]
21
22
V :: Rx : MakeUnrx < Unrx = V > + Freeze + Clone ;
22
23
/// The reactive version of [`RxHashMapNested`].
23
24
#[ derive( Clone , Debug ) ]
24
- pub struct RxHashMapNestedRx < K , V > ( HashMap < K , V :: Rx > )
25
+ pub struct RxHashMapNestedRx < K , V > ( RcSignal < HashMap < K , V :: Rx > > )
25
26
where
26
27
K : Clone + Serialize + DeserializeOwned + Eq + Hash ,
27
28
V : MakeRx + Serialize + DeserializeOwned + ' static ,
37
38
type Rx = RxHashMapNestedRx < K , V > ;
38
39
39
40
fn make_rx ( self ) -> Self :: Rx {
40
- RxHashMapNestedRx ( self . 0 . into_iter ( ) . map ( |( k, v) | ( k, v. make_rx ( ) ) ) . collect ( ) )
41
+ RxHashMapNestedRx ( create_rc_signal (
42
+ self . 0 . into_iter ( ) . map ( |( k, v) | ( k, v. make_rx ( ) ) ) . collect ( ) ,
43
+ ) )
41
44
}
42
45
}
43
46
impl < K , V > MakeUnrx for RxHashMapNestedRx < K , V >
@@ -49,17 +52,15 @@ where
49
52
type Unrx = RxHashMapNested < K , V > ;
50
53
51
54
fn make_unrx ( self ) -> Self :: Unrx {
52
- RxHashMapNested (
53
- self . 0
54
- . into_iter ( )
55
- . map ( |( k, v) | ( k, v. make_unrx ( ) ) )
56
- . collect ( ) ,
57
- )
55
+ let map = ( * self . 0 . get_untracked ( ) ) . clone ( ) ;
56
+ RxHashMapNested ( map. into_iter ( ) . map ( |( k, v) | ( k, v. make_unrx ( ) ) ) . collect ( ) )
58
57
}
59
58
60
59
#[ cfg( client) ]
61
60
fn compute_suspense ( & self , cx : Scope ) {
62
- for elem in self . 0 . values ( ) {
61
+ // We do *not* want to recompute this every time the user changes the state!
62
+ // (There lie infinite loops.)
63
+ for elem in self . 0 . get_untracked ( ) . values ( ) {
63
64
elem. compute_suspense ( cx) ;
64
65
}
65
66
}
83
84
V : MakeRx + Serialize + DeserializeOwned + ' static ,
84
85
V :: Rx : MakeUnrx < Unrx = V > + Freeze + Clone ,
85
86
{
86
- type Target = HashMap < K , V :: Rx > ;
87
+ type Target = RcSignal < HashMap < K , V :: Rx > > ;
87
88
88
89
fn deref ( & self ) -> & Self :: Target {
89
90
& self . 0
0 commit comments