BackgroundThreadStateHandler


@UnstableApi
class BackgroundThreadStateHandler<T : Any?>


Helper class to handle state updates on a background thread while maintaining a placeholder state on the foreground thread.

Parameters
<T : Any?>

An immutable object representing the entire state. Must implement equals.

Summary

Nested types

An interface to handle changes to the state on the foreground thread.

Public constructors

BackgroundThreadStateHandler(
    initialState: T!,
    backgroundLooper: Looper!,
    foregroundLooper: Looper!,
    clock: Clock!,
    onStateChanged: BackgroundThreadStateHandler.StateChangeListener<T!>!
)

Creates the helper for background thread state updates.

Public functions

T!
get()

Returns the current state.

Unit

Runs the provided Runnable on the background thread.

Unit
setStateInBackground(newState: T!)

Updates the background state directly, independent to any operation started from the foreground thread.

Unit
updateStateAsync(
    placeholderState: Function<T!, T!>!,
    backgroundStateUpdate: Function<T!, T!>!
)

Starts an asynchronous state update.

Public constructors

BackgroundThreadStateHandler

BackgroundThreadStateHandler(
    initialState: T!,
    backgroundLooper: Looper!,
    foregroundLooper: Looper!,
    clock: Clock!,
    onStateChanged: BackgroundThreadStateHandler.StateChangeListener<T!>!
)

Creates the helper for background thread state updates.

This constructor may be called on any thread.

Parameters
initialState: T!

The initial state value.

backgroundLooper: Looper!

The Looper to run background operations on.

foregroundLooper: Looper!

The Looper to run foreground operations on.

clock: Clock!

The Clock to control the handler messages.

onStateChanged: BackgroundThreadStateHandler.StateChangeListener<T!>!

The StateChangeListener to listen to state changes.

Public functions

get

fun get(): T!

Returns the current state.

Can be called on either the foreground or background thread, returning the respective current state of this thread.

runInBackground

fun runInBackground(runnable: Runnable!): Unit

Runs the provided Runnable on the background thread.

Can be called from any thread.

Note: This method is useful to update the state on the background using setStateInBackground for events arriving from external sources. Use updateStateAsync if the intention is update the state in response the a foreground thread method call.

Parameters
runnable: Runnable!

The Runnable to be called on the background thread.

setStateInBackground

fun setStateInBackground(newState: T!): Unit

Updates the background state directly, independent to any operation started from the foreground thread.

Must only be called on the background thread.

Parameters
newState: T!

The new state.

updateStateAsync

fun updateStateAsync(
    placeholderState: Function<T!, T!>!,
    backgroundStateUpdate: Function<T!, T!>!
): Unit

Starts an asynchronous state update.

Must only be called on the foreground thread.

Parameters
placeholderState: Function<T!, T!>!

A function to create a placeholder state from the current state while the operation is pending. Will be called on the foreground thread.

backgroundStateUpdate: Function<T!, T!>!

A function to handle the background state update, taking in the current background state and returning the updated state. Will be called on the background thread.