MiddlewareRegistry

This registry allows you to keep track of multiple middleware and multiple contexts. It defines a single level hierarchy where there is the parent context, and optional children contexts.

You can define a parent MiddlewareContext for the registry. Children middleware will have access to the parent context as long as they are registered and the parent context is attached. You can also define MiddlewareContextFactory which is used to construct context from the parent MiddlewareContext. The child context can not have a lifecycle outside of the parent context; in other words, when the parent context is detached, all the children are detached. You control the lifecycle of the children context with the register, unregister functions.

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard

Returns current state of middleware, true if already attached, false otherwise

Link copied to clipboard
val middlewares: StateFlow<Set<Middleware<*>>>

A flow representing the current set of registered middleware instances.

Functions

Link copied to clipboard
fun context(): Flow<Context?>
Link copied to clipboard
fun detach()

Function called to detach middleware context if it exists

Link copied to clipboard
fun <R> execute(block: Context.() -> R): R?

Executes a block within the middleware context if available

Link copied to clipboard
fun <M : Middleware<Context>> getMiddleware(clazz: KClass<out M>): M?

Retrieves a single middleware instance of a given class type.

Link copied to clipboard
fun <M : Middleware<Context>> getMiddlewares(clazz: KClass<out M>): List<M>

Retrieves all middleware instances of a given class type

Link copied to clipboard
fun launchOnAttached(block: suspend Context.() -> Unit): Job

Launches a suspending block when the registry is attached

Link copied to clipboard
Link copied to clipboard
fun <M : Middleware<Context>> observeMiddlewares(clazz: KClass<out M>): Flow<Set<M>>

Observes middleware instances of a given class type.

Link copied to clipboard
open override fun onAttached(middlewareContext: Context)

Attaches all registered middleware instances when the registry is attached

Link copied to clipboard
open override fun onDetached(middlewareContext: Context)

Detaches all registered middleware instances when the registry is detached

Link copied to clipboard

Registers a middleware instance

Registers a middleware instance with a context factory

Link copied to clipboard
suspend fun repeatOnAttached(block: suspend Context.() -> Unit)

Executes a suspending block whenever the registry is attached

Link copied to clipboard

Unregisters a middleware instance

Link copied to clipboard

Unregisters all middleware instances.