
commonMain.me.aartikov.replica.paged.PagedPhysicalReplica.kt Maven / Gradle / Ivy
package me.aartikov.replica.paged
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import me.aartikov.replica.common.InvalidationMode
import me.aartikov.replica.common.OptimisticUpdate
import me.aartikov.replica.common.ReplicaId
import me.aartikov.replica.common.ReplicaTag
interface PagedPhysicalReplica> : PagedReplica> {
/**
* Unique identifier
*/
val id: ReplicaId
/**
* Human readable name, used for debugging
*/
val name: String
/**
* Settings, see: [PagedReplicaSettings]
*/
val settings: PagedReplicaSettings
/**
* Tags that can be used for bulk operations
*/
val tags: Set
/**
* A coroutine scope that represents life time of a replica.
*/
val coroutineScope: CoroutineScope
/**
* Provides [PagedReplicaState] as an observable value.
*/
val stateFlow: StateFlow>
/**
* Notifies that some [PagedReplicaEvent] has occurred.
*/
val eventFlow: Flow>
/**
* Replace current data with new [data].
*
* Note: It doesn't change data freshness. If previous data is missing a new data will be stale.
*/
suspend fun setData(data: List)
/**
* Modifies current data with [transform] function if it is exists.
*
* Note: It doesn't change data freshness.
*/
suspend fun mutateData(transform: (List
) -> List
)
/**
* Makes data stale if it is exists. It also could call a refresh depending on [InvalidationMode].
*/
suspend fun invalidate(mode: InvalidationMode = InvalidationMode.RefreshIfHasObservers)
/**
* Makes data fresh if it is exists.
*/
suspend fun makeFresh()
/**
* Cancels current request if it is in progress.
*/
fun cancel()
/**
* Cancels current request and clears data.
*/
suspend fun clear()
/**
* Clears error stored in [PagedReplicaState].
*/
suspend fun clearError()
/**
* Begins optimistic update. Observed data will be transformed by [update] function immediately.
*
* Note: for simple cases it is better to use [withOptimisticUpdate] extension.
*/
suspend fun beginOptimisticUpdate(update: OptimisticUpdate>)
/**
* Commits optimistic update. Replica forgets previous data.
*
* Note: for simple cases it is better to use [withOptimisticUpdate] extension.
*/
suspend fun commitOptimisticUpdate(update: OptimisticUpdate>)
/**
* Rollbacks optimistic update. Observed data will be replaced to the original one.
*
* Note: for simple cases it is better to use [withOptimisticUpdate] extension.
*/
suspend fun rollbackOptimisticUpdate(update: OptimisticUpdate>)
}
/**
* Returns current [PagedReplicaState].
*/
val > PagedPhysicalReplica.currentState get() = stateFlow.value