BlocRegistry

A centralised, type-safe registry for StateEmitter instances (both Bloc and Cubit).

BlocRegistry is the service locator that backs BlocProvider. You rarely interact with it directly; instead, wrap your composables with BlocProvider and resolve blocs where you need them.

Overview

// Registration — done automatically by BlocProvider
BlocProvider(listOf(CounterBloc(), AuthBloc())) {
AppContent()
}

// Resolution — anywhere in the composable tree
val counterBloc = BlocRegistry.resolve(CounterBloc::class)

Error handling

resolve throws an IllegalStateException with a descriptive message if the requested type was not registered. This fail-fast behaviour catches configuration mistakes at development time.

Thread safety

All operations are expected to run on the main thread, consistent with the rest of the Bloc library.

Functions

Link copied to clipboard
fun closeAll()

Closes all registered emitters and clears the store.

Link copied to clipboard
fun register(blocs: List<StateEmitter<*>>)

Registers a list of StateEmitter instances, replacing any previous registrations.

fun register(vararg blocs: StateEmitter<*>)

Convenience overload accepting varargs.

Link copied to clipboard

Calls AnyHydratedBloc.resetToInitialState on every registered HydratedBloc, then wipes the entire HydratedBloc.storage.

Link copied to clipboard
fun <T : StateEmitter<*>> resolve(type: KClass<T>): T

Resolves a StateEmitter by its concrete KClass.