From 28703f1efab49c74e2e7f649ae09c8e1d0697277 Mon Sep 17 00:00:00 2001 From: jtdLab <72231111+jtdLab@users.noreply.github.com> Date: Tue, 15 Aug 2023 11:29:04 +0200 Subject: [PATCH 1/2] feat(bloc): make emit in `BlocBase` an `Emitter` --- packages/bloc/lib/src/bloc.dart | 2 +- packages/bloc/lib/src/bloc_base.dart | 36 ++++++++++++++-------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/bloc/lib/src/bloc.dart b/packages/bloc/lib/src/bloc.dart index e2acd592439..fd0c7abe382 100644 --- a/packages/bloc/lib/src/bloc.dart +++ b/packages/bloc/lib/src/bloc.dart @@ -151,7 +151,7 @@ abstract class Bloc extends BlocBase /// {@endtemplate} @visibleForTesting @override - void emit(State state) => super.emit(state); + Emitter get emit => super.emit; /// Register event handler for an event of type `E`. /// There should only ever be one event handler per event type `E`. diff --git a/packages/bloc/lib/src/bloc_base.dart b/packages/bloc/lib/src/bloc_base.dart index 915624e8747..95bb8fa0404 100644 --- a/packages/bloc/lib/src/bloc_base.dart +++ b/packages/bloc/lib/src/bloc_base.dart @@ -28,11 +28,11 @@ abstract class Closable { bool get isClosed; } -/// An object that can emit new states. +/// An object that can emit new states via the [Emitter]. // ignore: one_member_abstracts abstract class Emittable { - /// Emits a new [state]. - void emit(State state); + /// The emitter. + Emitter get emit; } /// A generic destination for errors. @@ -91,21 +91,21 @@ abstract class BlocBase @protected @visibleForTesting @override - void emit(State state) { - try { - if (isClosed) { - throw StateError('Cannot emit new states after calling close'); - } - if (state == _state && _emitted) return; - onChange(Change(currentState: this.state, nextState: state)); - _state = state; - _stateController.add(_state); - _emitted = true; - } catch (error, stackTrace) { - onError(error, stackTrace); - rethrow; - } - } + Emitter get emit => _Emitter((state) { + try { + if (isClosed) { + throw StateError('Cannot emit new states after calling close'); + } + if (state == _state && _emitted) return; + onChange(Change(currentState: this.state, nextState: state)); + _state = state; + _stateController.add(_state); + _emitted = true; + } catch (error, stackTrace) { + onError(error, stackTrace); + rethrow; + } + }); /// Called whenever a [change] occurs with the given [change]. /// A [change] occurs when a new `state` is emitted. From 97baf157bea89c4bd4dd09025312a9a566fefd89 Mon Sep 17 00:00:00 2001 From: jtdLab <72231111+jtdLab@users.noreply.github.com> Date: Tue, 15 Aug 2023 11:33:31 +0200 Subject: [PATCH 2/2] fix(replay_bloc): solve incompatibility with bloc --- packages/bloc/lib/src/emitter.dart | 3 + packages/replay_bloc/lib/src/replay_bloc.dart | 72 +++++++++---------- .../replay_bloc/lib/src/replay_cubit.dart | 22 +++--- 3 files changed, 50 insertions(+), 47 deletions(-) diff --git a/packages/bloc/lib/src/emitter.dart b/packages/bloc/lib/src/emitter.dart index 0b5a102a43c..a6effca50a8 100644 --- a/packages/bloc/lib/src/emitter.dart +++ b/packages/bloc/lib/src/emitter.dart @@ -9,6 +9,9 @@ part of 'bloc.dart'; /// /// {@endtemplate} abstract class Emitter { + /// {@macro emitter} + factory Emitter(void Function(State state) emit) => _Emitter(emit); + /// Subscribes to the provided [stream] and invokes the [onData] callback /// when the [stream] emits new data. /// diff --git a/packages/replay_bloc/lib/src/replay_bloc.dart b/packages/replay_bloc/lib/src/replay_bloc.dart index c3d56e4663e..159a4ded0f7 100644 --- a/packages/replay_bloc/lib/src/replay_bloc.dart +++ b/packages/replay_bloc/lib/src/replay_bloc.dart @@ -96,42 +96,42 @@ mixin ReplayBlocMixin on Bloc { } @override - void emit(State state) { - _changeStack.add( - _Change( - this.state, - state, - () { - final event = _Redo(); - onEvent(event); - onTransition( - Transition( - currentState: this.state, - event: event, - nextState: state, - ), - ); - // ignore: invalid_use_of_visible_for_testing_member - super.emit(state); - }, - (val) { - final event = _Undo(); - onEvent(event); - onTransition( - Transition( - currentState: this.state, - event: event, - nextState: val, - ), - ); - // ignore: invalid_use_of_visible_for_testing_member - super.emit(val); - }, - ), - ); - // ignore: invalid_use_of_visible_for_testing_member - super.emit(state); - } + Emitter get emit => Emitter((state) { + _changeStack.add( + _Change( + this.state, + state, + () { + final event = _Redo(); + onEvent(event); + onTransition( + Transition( + currentState: this.state, + event: event, + nextState: state, + ), + ); + // ignore: invalid_use_of_visible_for_testing_member + super.emit(state); + }, + (val) { + final event = _Undo(); + onEvent(event); + onTransition( + Transition( + currentState: this.state, + event: event, + nextState: val, + ), + ); + // ignore: invalid_use_of_visible_for_testing_member + super.emit(val); + }, + ), + ); + // ignore: invalid_use_of_visible_for_testing_member + super.emit(state); + }); /// Undo the last change. void undo() => _changeStack.undo(); diff --git a/packages/replay_bloc/lib/src/replay_cubit.dart b/packages/replay_bloc/lib/src/replay_cubit.dart index 6c430cfcfed..d050a0384cc 100644 --- a/packages/replay_bloc/lib/src/replay_cubit.dart +++ b/packages/replay_bloc/lib/src/replay_cubit.dart @@ -63,17 +63,17 @@ mixin ReplayCubitMixin on Cubit { set limit(int limit) => _changeStack.limit = limit; @override - void emit(State state) { - _changeStack.add( - _Change( - this.state, - state, - () => super.emit(state), - (val) => super.emit(val), - ), - ); - super.emit(state); - } + Emitter get emit => Emitter((state) { + _changeStack.add( + _Change( + this.state, + state, + () => super.emit(state), + (val) => super.emit(val), + ), + ); + super.emit(state); + }); /// Undo the last change. void undo() => _changeStack.undo();