-
Notifications
You must be signed in to change notification settings - Fork 451
/
Copy.kt
32 lines (28 loc) · 883 Bytes
/
Copy.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package arrow.optics
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.snapshots.Snapshot
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
/**
* Modifies the value in this [MutableState]
* by applying the function [block] to the current value.
*/
public inline fun <T> MutableState<T>.update(crossinline block: (T) -> T) {
Snapshot.withMutableSnapshot {
value = block(value)
}
}
/**
* Modifies the value in this [MutableState]
* by performing the operations in the [Copy] [block].
*/
public fun <T> MutableState<T>.updateCopy(block: Copy<T>.() -> Unit) {
update { it.copy(block) }
}
/**
* Updates the value in this [MutableStateFlow]
* by performing the operations in the [Copy] [block].
*/
public fun <T> MutableStateFlow<T>.updateCopy(block: Copy<T>.() -> Unit) {
update { it.copy(block) }
}