ReversibleState
1.0.0indexedTracks editable and applied values, exposes an uncommitted-change indicator, supports commit/revert operations, and grouping to commit or revert only dirty child states.
Tracks editable and applied values, exposes an uncommitted-change indicator, supports commit/revert operations, and grouping to commit or revert only dirty child states.
Reversible state holders for Kotlin Multiplatform.
ReversibleState tracks an editable value alongside an applied value, exposes whether there are uncommitted changes, and provides commit/revert operations for pushing or discarding edits.
repositories {
mavenCentral()
}
dependencies {
implementation("io.github.w2sv:reversible-state:<version>")
}
For Kotlin Multiplatform projects:
kotlin {
sourceSets {
commonMain.dependencies {
implementation("io.github.w2sv:reversible-state:<version>")
}
}
}
import com.w2sv.reversiblestate.ReversibleStateFlow
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
{
appliedState = MutableStateFlow()
state = ReversibleStateFlow(
scope = scope,
appliedState = appliedState,
commitState = { commit -> appliedState.value = commit.current }
)
state.value =
state.update { }
hasPendingChanges = state.isDirty.value
committed = state.commit()
state.value =
state.revert()
}
ReversibleStateFlow implements StateFlow<T> for collection and synchronous reads, with editing through writable
value and atomic update(). isDirty.value immediately compares the draft with the trusted persisted baseline.
serializes callers and returns after a new successful write or when no write is needed.
Edits and revert remain responsive during persistence. Revert restores the trusted baseline, even if the applied state
emission for a successful commit is delayed.
StateCommit(previous, current) contains the trusted baseline and draft captured for a write. Successful commits emit
this transition on commits, a replay-free SharedFlow for transient notifications. Inactive or slow collectors may
miss events, so keep critical side effects out of this flow. Keep commitState focused on persistence: if it writes and
then fails, the library may retry the write.
Before the first local persistence attempt, the default AppliedStateSyncPolicy.WhenUnmodified follows applied state
changes only when no newer local editing intent exists. Always copies every emission into the draft; Never leaves
the draft alone. After a persistence attempt, delayed emissions cannot be reliably distinguished from newer external
writes, so they cannot change the trusted baseline and WhenUnmodified stops automatic synchronization. Recreate the
editor from a freshly loaded applied state when resynchronization is needed. previous may differ from the exact value
overwritten by a concurrent external writer.
ReversibleStateComposition groups several states. Its aggregate isDirty.value is synchronous. It serializes
composition commits, returns whether any child wrote, and calls onCommit only with children whose commit() returned
true. Earlier children remain persisted if a later child fails; their writes are not one atomic transaction.
Apache License 2.0. See LICENSE.
commit()truefalseSurfaced from shared tags and platforms — no rankings paid for.