| type | API Reference | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| title | API reference | ||||||||||||||
| description | Every public type and function in com.umain.revolver and com.umain.revolver.flow. | ||||||||||||||
| tags |
|
||||||||||||||
| resource | https://github.com/apegroup/revolver/tree/main/revolver/src | ||||||||||||||
| status | stable | ||||||||||||||
| generated |
|
||||||||||||||
| sources |
|
Core types are in com.umain.revolver. Flow wrappers are in com.umain.revolver.flow.1
interface RevolverEvent
interface RevolverState
interface RevolverEffectImplement each one with a sealed class. See Core concepts.
typealias StateEmitter<STATE> = (state: STATE) -> Unit
typealias EffectEmitter<EFFECT> = (effect: EFFECT) -> Unit
interface Emitter<STATE, EFFECT> {
val state: StateEmitter<STATE> // sets the current state immediately
val effect: EffectEmitter<EFFECT> // delivers a one-shot effect asynchronously
}
typealias EventHandler<EVENT, STATE, EFFECT> = suspend (event: EVENT, emit: Emitter<STATE, EFFECT>) -> Unit
typealias ErrorHandler<ERROR, STATE, EFFECT> = suspend (exception: ERROR, emit: Emitter<STATE, EFFECT>) -> UnitThe public contract of a ViewModel. Code that uses a ViewModel can depend on this interface instead of the concrete class.
| Member | Type | Description |
|---|---|---|
state |
CStateFlow<STATE> |
Current state. Always holds a value. |
effect |
CSharedFlow<EFFECT> |
One-shot effects. Not replayed. |
emit(event) |
(EVENT) -> Unit |
Queues an event for processing. |
open class. It implements RevolverViewModelInterface and extends BaseViewModel.
| Function | Behaviour |
|---|---|
addEventHandler<T : EVENT>(handler: EventHandler<T, STATE, EFFECT>) |
Registers the handler for event type T. Throws IllegalStateException if T already has a handler. |
addErrorHandler<ERROR : Throwable>(handler: ErrorHandler<ERROR, STATE, EFFECT>) |
Registers a handler for exceptions of type ERROR. Handlers are tried in registration order, and the first match wins. Throws IllegalStateException if ERROR already has a handler. |
addErrorHandler<ERROR : Throwable>(errorHandler: RevolverErrorHandler<STATE, EFFECT, ERROR>) |
Same as above, for a reusable handler object. |
emit(event: EVENT) |
Puts the event on a 64-slot buffered channel. Events are handled one at a time, in order. Throws IllegalStateException if the channel can't accept the event. |
internalEventHandler and internalErrorHandler are public only because the inline functions above call them. Don't call them directly.
interface RevolverErrorHandler<STATE, EFFECT, ERROR> {
suspend fun handleError(exception: ERROR, emit: Emitter<STATE, EFFECT>)
}A RevolverErrorHandler for Throwable. It logs the exception through Napier at error level and emits genericErrorState.
expect open class that provides viewModelScope: CoroutineScope, running on Dispatchers.Main.
| Platform | Lifecycle |
|---|---|
| Android | Extends AndroidX ViewModel. The scope is cancelled in onCleared(). |
| iOS | Call dispose() to cancel the scope.2 |
| Type | Wraps | Exposed as |
|---|---|---|
CFlow<T : Any>(flow, coroutineScope) |
Flow<T> |
— |
CSharedFlow<T : Any>(flow, coroutineScope) |
SharedFlow<T> |
effect |
CStateFlow<T : Any>(flow, coroutineScope) |
StateFlow<T> |
state |
Extension builders: Flow<T>.cFlow(), SharedFlow<T>.cSharedFlow() and StateFlow<T>.cStateFlow(). Each takes an optional coroutineScope, which defaults to GlobalScope.
On Android the wrappers just delegate to the wrapped flow. On iOS they add:
| Member | Description |
|---|---|
watch(onNext: (T) -> Unit): DisposableHandle |
Collects on Dispatchers.Main in the wrapper's scope. |
interface DisposableHandle : kotlinx.coroutines.DisposableHandle |
dispose() cancels the observation. |
operator fun DisposableHandle.plus(other) |
Returns a handle that disposes both. |
See iOS integration for usage.