StatefulComputedValue
Computed value for values of type T where the form manager maintains a computed value state of type TState for each managed value.
Stateful computed values are useful when computed values require an expensive computation over data and if it is possible to save the result of such expensive computation and tweak it as data changes instead of running the expensive computation all over again.
As an example, imagine that we have a list of people and that we want to compute the average age of all people. Instead of iterating over the whole list every time a person is added or removed, we can use a stateful computed value to save, as state, the sum of all ages, and simply tweak this sum as people are added or removed. Having access to the sum of all ages as state allows us to implement the computed value function with a complexity of O(1) as opposed to O(N).
The following snippet implements the example above:
object AvgAgeNotOverMax : StatefulComputedValue<Int, Int>() {
private val ComputedValueContext.people: List<Person> by dependency("../people")
override suspend fun ComputedValueContext.initState(): Int =
people.fold(0) { sum, person -> sum + person.age }
private val ageObserver by observe<Int>("../people/∗/age") { agesSum, event ->
when (event) {
is ValueEvent.Init<Int> -> agesSum + event.newValue
is ValueEvent.Change<Int> -> agesSum + event.newValue - event.oldValue
is ValueEvent.Destroy<Int> -> agesSum - event.oldValue
else -> agesSum
}
}
override suspend fun ComputedValueContext.computeFromState(state: Int) = state / people.size
}Properties
Dependencies of the computed value. Mapping of keys to the paths this computed value depends on. Keys can be used within a ComputedValueContext to access the value of the dependencies.
Set of external context dependencies of the computed value.
Schema of the value being computed.
Functions
Declares a dependency to path, accessible in the computed value's context via key dependencyKey.
Declares an external context dependency to externalContextName.
Adds an observer to the path observer.toObserve to update the computed value state via observer.updateState whenever an event with a path matching observer.toObserve occurs.
Adds an observer to the path pathToObserve to update the computed value state via updateState whenever an event with a path matching pathToObserve occurs.
Runs the computation within a ComputedValueContext containing the value of all declared dependencies. Returns the computed value.
Runs the computation, given its state, within a ComputedValueContext containing the value of all declared dependencies. Returns the computed value.
Function used to declare a dependency to a path and delegate access to its value within a ComputedValueContext.
Function used to declare a dependency to a path and delegate access to its value within a ComputedValueContext.
Destroys the computed value's state.
Function used to declare a dependency to an external context and delegate access to its value within a ComputedValueContext.
Function used to declare a dependency to an external context and delegate access to its value within a ComputedValueContext.
Initialises and returns the computed value's state, within a ComputedValueContext containing the values of all declared dependencies.
Function used to observe the path pathToObserve and update the computed value state via updateState whenever an event with a path matching pathToObserve occurs.