felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.84k stars 3.4k forks source link

feat: BlocObserver with onDone callback #3148

Open PlugFox opened 2 years ago

PlugFox commented 2 years ago

Add callback inside BLoC for BlocObserver:

void onDone(Bloc bloc, Object? event, Object? error, StackTrace? stackTrace)

or rename onEvent to onEventStarted and add onEventFinished onDone event must be always called if onEvent called, no matter if an error occurred or not.

This is the only proposal for the main idea of the wanted API.

Why do I want this feature? Because is very useful to know, how long events are executed and successful or not.

I want integrate it with analytics and Sentry transactions:

final tx = Sentry.startTransaction(bloc, event);
// ...
tx.finish(status: SpanStatus.ok());

And get reports something like this: image

Because BLoC's are always root for every business logic - this is very handy data. We can know, how often users doing some events/features, how long they are processing. Rate successful/error. What's error occurred inside BLoCs and events.

PlugFox commented 2 years ago

Also, you can change API for BLoC Observer to Interceptors list.

Something like that:

BlocObserver.instance..addAll(
  <IBlocBaseInterceptor>[
     CustomCubitInterceptor(),
     CustomBlocInterceptor(),
     CustomEventInterceptor(),
     CustomTransationtInterceptor(),
     CustomBlocErrorHandlerInterceptor(),
  ]
);

I think this is more handy and scale for every users)

Community can separate logic for:

PlugFox commented 2 years ago

Another idea: you can simply add in Transition class: bool get completed; If completed is true - this is last state for this event.

yeleibo commented 9 months ago

If this is implemented, I can uniformly handle the pop-up windows and pop-up window closing of network requests. Hope it can be realized soon. Thank you so much. The temporary compromise is to create a pop-up window in onevent and close the pop-up window in onTransition @felangel

lrampazzo commented 2 months ago

Hi @felangel, any plans on this? Possibly you will accept a PR?