Package me.aartikov.replica.single

Types

Link copied to clipboard
fun interface Fetcher<T : Any>

Fetches data from a server. Throws exception on error.

Link copied to clipboard
data class Loadable<out T : Any>(    val loading: Boolean = false,     val data: T? = null,     val error: CombinedLoadingError? = null)

Replica state that can be observed on a UI. In opposite to ReplicaState this class contains very limited set of fields.

Link copied to clipboard
interface PhysicalReplica<T : Any> : Replica<T>

Replica is a primitive for data replication. The replica's task is to represent some chunk of data from a server on a client side. Replica is configured by Fetcher and ReplicaSettings. Replica loads missing data when an active observer connects (see: Replica.observe). Replica keeps track of data staleness. Replica refreshes stale data when an active observer is connected. Replica deduplicates network requests (it doesn't coll a new request if another one is in progress). Replica cancels network request when a last observer is disconnected. Replica clears data when it has no observers for a long time.

Link copied to clipboard
interface Replica<out T : Any>

Replica is a primitive for data replication. The replica's task is to represent some chunk of data from a server on a client side. Replica is configured by Fetcher and ReplicaSettings. Replica loads missing data when an active observer connects (see: Replica.observe). Replica keeps track of data staleness. Replica refreshes stale data when an active observer is connected. Replica deduplicates network requests (it doesn't coll a new request if another one is in progress). Replica cancels network request when a last observer is disconnected. Replica clears data when it has no observers for a long time.

Link copied to clipboard
data class ReplicaData<T : Any>(    val value: T,     val fresh: Boolean,     val changingTime: Instant,     val optimisticUpdates: List<OptimisticUpdate<T>> = emptyList())

Data stored in a replica.

Link copied to clipboard
interface ReplicaEvent<out T : Any>

Notifies that something happened in PhysicalReplica.

Link copied to clipboard
interface ReplicaObserver<out T : Any>

Replica observer connects to a replica and receives updates: state and loading error events. Replica observer can be active or inactive (this property is not exposed in the API but is used internally). Only active observer receives updates. Replica observer is associated with some User Interface - a screen or a part of screen.

Link copied to clipboard
data class ReplicaSettings(    val staleTime: Duration?,     val clearTime: Duration? = null,     val clearErrorTime: Duration? = 250.milliseconds,     val cancelTime: Duration? = 250.milliseconds,     val revalidateOnActiveObserverAdded: Boolean = true,     val revalidateOnNetworkConnection: Boolean = true)

Configures behaviour of a replica.

Link copied to clipboard
data class ReplicaState<T : Any>(    val loading: Boolean,     val data: ReplicaData<T>?,     val error: LoadingError?,     val observingState: ObservingState,     val dataRequested: Boolean,     val preloading: Boolean,     val loadingFromStorageRequired: Boolean)

State of PhysicalReplica.

Link copied to clipboard
interface Storage<T : Any>

Allows PhysicalReplica to save data on a persistent storage.

Functions

Link copied to clipboard
fun <T : Any, R : Any> Loadable<T>.mapData(transform: (T) -> R): Loadable<R>

Transforms data with a transform functions.

Link copied to clipboard
inline suspend fun <T : Any, R> withOptimisticUpdate(    update: OptimisticUpdate<T>,     replica: PhysicalReplica<T>,     noinline onSuccess: suspend () -> Unit? = null,     noinline onError: suspend (Exception) -> Unit? = null,     noinline onCanceled: suspend () -> Unit? = null,     noinline onFinished: suspend () -> Unit? = null,     block: () -> R): R

Executes an optimistic update on a replica. update is applied immediately on observed replica state. Then block is executed. If block succeed an update is committed, otherwise an update is rolled back.

Properties

Link copied to clipboard
val <T : Any> PhysicalReplica<T>.currentState: ReplicaState<T>

Returns current ReplicaState.

val <T : Any> ReplicaObserver<T>.currentState: Loadable<T>

Returns current replica state.