-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(bloc): Extract base bloc interface #2453
Conversation
@gocal thanks for the contribution! I'll try to get this merged shortly but unfortunately, I believe it is a breaking change because developers could be extending |
Going to close this PR since it's quite out-dated and has lots of merge conflicts -- this will be addressed as part of #2896 and included in the upcoming v8.0.0 release. Thanks again! |
@gocal sorry for the delay but I'm revisiting this and I was hoping you could clarify the issue you were facing with the current implementation.
There is no You should be able to define an mixin EffectBloc<State, Effect> on BlocBase<State> {
StreamController<Effect>? __effectController;
StreamController<Effect> get _effectController {
return __effectController ??= StreamController<Effect>.broadcast();
}
Stream<Effect> get effectStream => _effectController.stream;
void emitEffect(Effect effect) {
if (_effectController.isClosed) return;
onEffect(effect);
_effectController.add(effect);
}
@mustCallSuper
void onEffect(Effect effect) {}
@mustCallSuper
@override
Future<void> close() async {
await _effectController.close();
return super.close();
}
} Then apply the mixin like: class MyEffectBloc extends Bloc<MyEffectEvent, MyEffectState>
with EffectBloc<MyEffectState, EffectType> {
// ...
} Let me know if that helps or if I misunderstood, thanks! |
Currently BlocBase is an abstract class so it's not possible to easily extend bloc functionality e.g. via custom mixins, Instead we always have to extend all the available implementations (bloc, cubit). Having the actual interface/contract instead of the abstract class guarantees bloc/cubit is independent from the BlocBase implementation.
I've create a small extensions to the bloc library that allows to use side effects that don't requires widget tree to be rebuilt.
https://github.com/allbrightio/effect_bloc/blob/main/examples/flutter_navigation/lib/view/login/login_bloc.dart
Status
READY
Breaking Changes
NO
Description
Converted BlocBase to an interface, and added default implementation of it (_BlocBaseImpl).
Since _BlocBaseImpl is not exposed outside this is not a breaking change.
Type of Change