1
1
package xyz.junerver.compose.hooks
2
2
3
3
import androidx.compose.runtime.Composable
4
- import androidx.compose.runtime.MutableState
5
4
import androidx.compose.runtime.Stable
5
+ import androidx.compose.runtime.State
6
6
import androidx.compose.runtime.remember
7
7
import androidx.compose.runtime.snapshots.SnapshotStateList
8
8
import kotlinx.collections.immutable.PersistentList
@@ -17,13 +17,6 @@ import kotlinx.collections.immutable.persistentListOf
17
17
* @Description
18
18
*/
19
19
20
- @Stable
21
- class ImmutableListHolder <T >(val list : MutableState <PersistentList <T >>) {
22
- fun mutate (mutator : (MutableList <T >) -> Unit ) {
23
- list.value = list.value.mutate(mutator)
24
- }
25
- }
26
-
27
20
/* *
28
21
* 这个 hook 不同于 [useList]。
29
22
*
@@ -37,5 +30,15 @@ class ImmutableListHolder<T>(val list: MutableState<PersistentList<T>>) {
37
30
@Composable
38
31
fun <T > useImmutableList (vararg elements : T ): ImmutableListHolder <T > {
39
32
val state = _useState (persistentListOf(* elements))
40
- return remember { ImmutableListHolder (state) }
33
+
34
+ fun mutate (mutator : (MutableList <T >) -> Unit ) {
35
+ state.value = state.value.mutate(mutator)
36
+ }
37
+ return remember { ImmutableListHolder (state, ::mutate) }
41
38
}
39
+
40
+ @Stable
41
+ data class ImmutableListHolder <T >(
42
+ val list : State <PersistentList <T >>,
43
+ val mutate : (mutator: (MutableList <T >) -> Unit ) -> Unit ,
44
+ )
0 commit comments