ValidatingStore

open class ValidatingStore<D, T, M>(initialData: D, validation: Validation<D, T, M>, validateAfterUpdate: Boolean, id: String) : RootStore<D>

A ValidatingStore is a RootStore which also contains a Validation for its model and by default applies it to every update.

This store is intentionally configured to validate the data on each update, that is why the validateAfterUpdate parameter is set to true by default.

There might be special situations where it is reasonable to disable this behaviour by setting validateAfterUpdate to false and to prefer applying the validation individually within custom handlers, for example if a model should only be validated after the user has completed his input or if metadata is needed for the validation process. Then be aware of the fact, that the call of the validate function actually updates the messages already.

If the new data is not passed to the store's state after validating it, the messages are probably out of sync with the actual store's state! This could lead to false assumptions and might produce hard to detect bugs in your application.

Parameters

initialData

first current value of this Store

validation

Validation function to use at the data on this Store.

validateAfterUpdate

flag to decide if a new value gets automatically validated after setting it to the Store.

id

id of this Store. Ids of SubStores will be concatenated.

Constructors

Link copied to clipboard
fun <D, T, M> ValidatingStore(initialData: D, validation: Validation<D, T, M>, validateAfterUpdate: Boolean = true, id: String = Id.next())

Functions

Link copied to clipboard
open suspend override fun enqueue(update: Update<D>)

in a RootStore an Update is handled by applying it to the internal StateFlow.

Link copied to clipboard
open fun errorHandler(cause: Throwable)

Default error handler printing the error to console.

Link copied to clipboard
open fun handle(execute: suspend (D) -> D): SimpleHandler<Unit>

Factory method to create a SimpleHandler that does not take an Action

open fun <A> handle(execute: suspend (D, A) -> D): SimpleHandler<A>

Factory method to create a SimpleHandler mapping the actual value of the Store and a given Action to a new value.

Link copied to clipboard
open fun <E> handleAndEmit(execute: suspend FlowCollector<E>.(D) -> D): EmittingHandler<Unit, E>

factory method to create an EmittingHandler that does not take an action in it's execute-lambda.

open fun <A, E> handleAndEmit(execute: suspend FlowCollector<E>.(D, A) -> D): EmittingHandler<A, E>

Factory method to create a EmittingHandler taking an action-value and the current store value to derive the new value. An EmittingHandler is a Flow by itself and can therefore be connected to other SimpleHandlers even in other Stores.

Link copied to clipboard
open infix fun <A> Flow<A>.handledBy(handler: Handler<A>)

Connects a Flow to a Handler.

open infix fun <A> Flow<A>.handledBy(execute: suspend (A) -> Unit): Job
open infix fun <E : Event> Flow<E>.handledBy(execute: suspend (E) -> Unit): Job

Connects a Flow to a suspendable execute function.

open infix fun <E : Event> Flow<E>.handledBy(handler: Handler<Unit>)

Connects Events to a Handler.

Link copied to clipboard
open operator fun Handler<Unit>.invoke()
open operator fun <A> Handler<A>.invoke(data: A)

Calls this handler exactly once.

Link copied to clipboard
open fun <X> sub(lens: Lens<D, X>): SubStore<D, X>

create a SubStore that represents a certain part of your data model.

Link copied to clipboard
open fun <D, I> Store<D>.syncWith(socket: Socket, resource: Resource<D, I>)

Syncs a Store by via a Websockets connection.

Properties

Link copied to clipboard
open override val current: D

Represents the current data of this RootStore.

Link copied to clipboard
override val data: Flow<D>

Emits a Flow with the current data of this RootStore. The Flow internal data is only changed, when the value differs from the last one to avoid calculations and updates that are not necessary.

Link copied to clipboard
open override val id: String
Link copied to clipboard
open override val job: Job

Job used as parent job on all coroutines started in Handlers in the scope of this Store

Link copied to clipboard
val messages: Flow<List<M>>

Flow of the List of validation-messages. Use this Flow to render out the validation-messages and to detect the valid state of the current data.

Link copied to clipboard
open override val path: String

Path of this Store derived from the underlying model. Paths of depending Stores are concatenated and separated by a dot.

Link copied to clipboard
open override val update: SimpleHandler<D>

a simple SimpleHandler that just takes the given action-value as the new value for the Store.

Link copied to clipboard
val validateAfterUpdate: Boolean = true