bloc icon indicating copy to clipboard operation
bloc copied to clipboard

feat: BlocObserver with onDone callback

Open PlugFox opened this issue 3 years ago • 4 comments

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 avatar Jan 18 '22 14:01 PlugFox

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:

  • Exceptions
  • Logging
  • Analytics
  • Metrics
  • Platform logic (Android / iOS / Web / etc)

PlugFox avatar Jan 18 '22 14:01 PlugFox

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

PlugFox avatar Jan 18 '22 16:01 PlugFox

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

yeleibo avatar Feb 05 '24 12:02 yeleibo