CachedValue

A plugin that allows to cache the value of a property, scoping it to the pro.respawn.flowmvi.api.Store's lifecycle. This plugin will clear the value when PipelineContext is canceled and call the init block each time a store is started to initialize it again.

This plugin is similar to lazy but where value is also bound to the pro.respawn.flowmvi.api.Store's lifecycle. This plugin can be used to get access to the PipelineContext and execute suspending operations in to initialize the value.

The init block can be called multiple times in rare cases of concurrent access. In practice, this should not happen

This plugin is useful with legacy APIs that rely on the kotlinx.coroutines.CoroutineScope to be present during the lifetime of a value, such as paging, and can be used to obtain the value in plugins such as whileSubscribedPlugin without recreating it.

The cached value must not be accessed before pro.respawn.flowmvi.api.StorePlugin.onStart and after pro.respawn.flowmvi.api.StorePlugin.onStop have been called, where it will be uninitialized. The init block is evaluated in pro.respawn.flowmvi.api.StorePlugin.onStart in the order the cache plugin was installed.

This plugin should not be used to run operations in PipelineContext. Use initPlugin for that.

Access the delegated property as follows:

// in store's scope
val pagedItems by cache { // this: PipelineContext ->
repo.pagedItems().cachedIn(this)
}

See also

Properties

Link copied to clipboard

Returns true if the cached value is present

Link copied to clipboard
val value: T

Obtain the value. The value can only be accessed before StorePlugin.onStart and StorePlugin.onStop, otherwise this function will throw!

Functions

Link copied to clipboard
open operator override fun getValue(thisRef: Any?, property: KProperty<*>): T