Adding support for atomic updates for two or more state field updates #12
PatilShreyas
started this conversation in
Ideas
Replies: 2 comments 1 reply
-
What do you think about changing the whole approach and have only one class StateImpl(val someProperty: Int): MutableState {
// TODO hide me with just StateFlow
val state = MutableStateFlow<State>(ImmutableState(someProperty))
var someProperty: Int
get() = state.value.someProperty
set(value) { state.update { it.copy(someProperty = value) } }
fun update(mutate: MutableState.() -> Unit) {
state.update { currentState ->
val changeTracker = object: MutableState {
// just keep track of updated properties and then combine them with currentState
var someProperty: Int = currentState.someProperty
}
mutate(changeTracker)
ImmutableState(changeTracker.someProperty) // keeps the current value or update with a new value
}
}
} I think it should be fairly easy to generate proper |
Beta Was this translation helpful? Give feedback.
1 reply
-
This approach has been released in |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Currently, the code generated by Mutekt looks like this.
As of v1.0.0-alpha02, it doesn't support atomic or transactional updates with two or more state fields. In this open discussion, we'll try to finalise an approach for supporting it (from issue #9).
So if model looks like this
The mutable model can have a method like follows:
Implementation of the mutable model interface can have the following changes:
Example of updating values atomically:
Explanation of these changes:
true
, then executes the lambda block in which developers can mutate the fields and then finally finishes the transaction flow by setting the value to `false.collect{}
of StateFlow's implementation, listen totransactionInProgress
as well. If the transaction is currently in progress and if we receive any state field changes, simply return a null. Else, emitsvalue
(which is implemented for calculating value on demand). Here, we are not creating an instance of state using fields which are passed in thecombine()
. Finally, filtering non-null values.update{}
of the model to update fields in a transaction which will trigger only single emission of state despite differentCoroutineDispatcher
behaviours.Question for discussion:
update{}
? How to rollback? Or do we even need to care of this?Beta Was this translation helpful? Give feedback.
All reactions