The project is motivated by dart's bloc library. Bloc design pattern helps to separate presentation from business logic.
A bloc relies on events to trigger states i.e a bloc receives events and convert the incoming events into outgoing states
A bloc observer listens to state changes of a bloc and it is intended to influence the presentation layer
Contains information of how the presentation layer looks like
Triggers the bloc which in turn provides new states
from bloc import Bloc
from enum import Enum
class Events(Enum):
increment = 0
decrement = 1
class CounterBloc(Bloc):
def __init__(self) -> None:
super().__init__(0)
self.on(Events.increment, self.__increment)
self.on(Events.decrement, self.__decrement)
def __increment(self, event, emit):
emit(self.current_state() + 1)
def __decrement(self, event, emit):
emit(self.current_state() - 1)
from bloc import bloc_test
def act(bloc):
bloc.add(Events.increment)
bloc.add(Events.increment)
bloc.add(Events.decrement)
bloc_test(
"Counter test",
bloc=CounterBloc(),
act=act,
expect=[1, 2, 1],
)
from bloc import BlocObserver
class MyObserver(BlocObserver):
def on_change(self, old_state, new_state):
print(f"MyObserver: old_state: {old_state} new_state: {new_state}")
bloc = CounterBloc()
bloc.set_observer(MyObserver())
bloc.add(Events.increment)
bloc.add(Events.decrement)
MyObserver: old_state: 0 new_state: 1
MyObserver: old_state: 1 new_state: 0