Bloc

abstract class Bloc<out State : Any, in Action : Any, SideEffect : Any> : BlocState<State, Action>

The core class of the framework. (can't be an interface or the generic types are erased in Swift)

A Bloc implements the app's business logic. It processes event data (called Action) from the view / ui component and:

  • updates State according to its business rules and emits the updated State to be consumed by the view / ui component.

  • optionally creates SideEffect(s) which can be consumed by the view / ui component (e.g. for navigation) or by other consumers (analytics events, logging etc.).

A Bloc is a BlocState (allowing us to use a Bloc as BlocState and create composable Blocs):

  • a StateStream emitting state

  • a Sink accepting actions that might trigger state changes

A Bloc has a SideEffectStream emitting side effects that can be used e.g. for navigation

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard

Converts a Bloc to a BlocState.

Link copied to clipboard
abstract suspend override fun collect(collector: FlowCollector<State>)

StateStream.collect(FlowCollector)

Link copied to clipboard
abstract fun observe(observerLifecycle: Lifecycle, state: BlocObserver<State>?, sideEffect: BlocObserver<SideEffect>?)

This observe function is used for iOS to ensure generic types aren't erased. It's the equivalent of the subscribe extension function for Android. Note: the state and sideEffect parameters are of type BlocObserver which is different from the signature used in the Android observe function.

Link copied to clipboard

Submit a Reducer without side effects to a Bloc to be run. The reducer will receive the state but no action (since it was triggered "manually", not by sending an action to the Bloc).

Link copied to clipboard

Submit a Reducer with side effects to a Bloc to be run. The reducer will receive the state but no action (since it was triggered "manually", not by sending an action to the Bloc).

Link copied to clipboard
abstract override fun send(action: Action)

Sink.send(Action)

Link copied to clipboard

Submit a SideEffect to a Bloc to be emitted. The side effect will receive the state but no action (since it was triggered "manually", not by sending an action to the Bloc). Note: the proposal is irrelevant for sideEffect so we set it to Unit

Link copied to clipboard
fun <State : Any, Action : Any, SideEffect : Any> Bloc<State, Action, SideEffect>.subscribe(lifecycleOwner: LifecycleOwner, state: suspend (state: State) -> Unit? = null, sideEffect: suspend (sideEffect: SideEffect) -> Unit? = null)

Same as above for a Bloc

fun <State : Any, Action : Any, SideEffect : Any> Bloc<State, Action, SideEffect>.subscribe(lifecycle: Lifecycle, state: suspend (state: State) -> Unit? = null, sideEffect: suspend (sideEffect: SideEffect) -> Unit? = null)

Subscribes to the state and side effects streams of a Bloc.

Link copied to clipboard

Submit a Thunk to a Bloc to be run. The thunk will receive the dispatch and the getState function but no action (since it was triggered "manually", not by sending an action to the Bloc). The dispatch function dispatches to the first matching thunk/reducer/side-effect in the Bloc.

Link copied to clipboard

If a components implements the BlocObservableOwner interface it needs to provide

Same as above but combine just two Blocs to BlocObservable.

Properties

Link copied to clipboard
Link copied to clipboard
abstract override val value: State

StateStream.value