MiddlewareScope

The scope that provides necessary operations for a middleware.

Functions

Link copied to clipboard
abstract fun dispatch(action: Action)

This is equivalent to Redux dispatch.

Link copied to clipboard
abstract fun next(action: Action)

Passes action to the next middleware in the pipeline.

Properties

Link copied to clipboard
abstract val closure: DispatchClosure

There is no equivalent to this in original Redux. It provides a current DispatchClosure. It remains immutable. However, elements inside might mutate depending on their implementation.

Link copied to clipboard
abstract val currentState: State

This is equivalent to Redux getState.

Extensions

Link copied to clipboard
inline fun <T : Action> MiddlewareScope<*>.consumingDispatch(crossinline block: (T) -> Unit): DispatchFunction

Creates a dispatch function that consumes actions with given supertype T. Other actions are passed to the next middleware.

Link copied to clipboard
val DispatchScope<*>.coroutineScope: CoroutineScope

Returns CoroutineScope associated with a store.

Link copied to clipboard

This function only returns given dispatch. It has a few benefits over simple DispatchFunction lambda that are illustrated with this example:

Link copied to clipboard
Link copied to clipboard

Dispatches action and expects any middleware to launch a single foreground job logically associate with it. Coroutine is launched in a scope provided by DispatchCoroutineScope.

Link copied to clipboard
fun DispatchScope<*>.dispatchJobIn(action: ForegroundJobAction, scope: CoroutineScope): Job

Dispatches action and expects any middleware to launch a single foreground job logically associate with it. Coroutine is launched in the scope.

Link copied to clipboard

Dispatches action and expects any middleware to launch a single foreground job logically associate with it. This function suspends until foreground job is finished. When coroutine that calls this function is cancelled, foreground job is also cancelled.

Link copied to clipboard
fun MiddlewareScope<*>.launchForeground(context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit): Job

Launches a foreground job using closure form this scope. By default, it's launched in a scope provided by DispatchCoroutineScope. This behaviour might be changed by dispatchJobIn or joinDispatchJob. Because this function uses local closure, calling it outside dispatch should not be done, because it might result in unexpected behaviour.

Link copied to clipboard
fun DispatchScope<*>.synchronized(block: suspend DispatchScope<*>.() -> Unit): Job

Runs given block as a coroutine in DispatchScope.coroutineScope. Effectively it ensures that any operation on a DispatchScope inside a block runs on proper thread.

Link copied to clipboard

Creates a dispatch function that executes a given block and passes every action to the next middleware.

Link copied to clipboard
inline fun <T : Action> MiddlewareScope<*>.translucentDispatchOf(crossinline block: (T) -> Unit): DispatchFunction

Creates a dispatch function that executes a given block for every action with given supertype T and passes every action to the next middleware.

Link copied to clipboard
inline fun <T> DispatchScope<*>.withLocalClosure(closure: DispatchClosure, newFrame: Boolean = false, block: DispatchScope<*>.() -> T): T

Adds local changes to LocalClosureContainer with a given closure for a time of block execution.