All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.arrow.optics.Copy.kt Maven / Gradle / Ivy

The newest version!
@file:JvmName("ComposeCopyKt")
@file:OptIn(ExperimentalContracts::class)

package arrow.optics

import androidx.compose.runtime.MutableState
import androidx.compose.runtime.snapshots.Snapshot
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.jvm.JvmName

/**
 * Modifies the value in this [MutableState]
 * by applying the function [block] to the current value.
 */
@Suppress("WRONG_INVOCATION_KIND") // withMutableSnapshot doesn't have a contract
public inline fun  MutableState.update(crossinline block: (T) -> T) {
  contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
  Snapshot.withMutableSnapshot {
    value = block(value)
  }
}

/**
 * Modifies the value in this [MutableState]
 * by performing the operations in the [Copy] [block].
 */
public fun  MutableState.updateCopy(block: Copy.() -> Unit) {
  update { it.copy(block) }
}

/**
 * Updates the value in this [MutableStateFlow]
 * by performing the operations in the [Copy] [block].
 */
public fun  MutableStateFlow.updateCopy(block: Copy.() -> Unit) {
  update { it.copy(block) }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy