diff --git a/packages/bloc/lib/src/bloc.dart b/packages/bloc/lib/src/bloc.dart index 252f2227a0c..89a979952c9 100644 --- a/packages/bloc/lib/src/bloc.dart +++ b/packages/bloc/lib/src/bloc.dart @@ -148,7 +148,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 5b9d87a3574..b5c09ad2703 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. @@ -90,21 +90,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. 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 5ad6298f404..dfb17e06cce 100644 --- a/packages/replay_bloc/lib/src/replay_bloc.dart +++ b/packages/replay_bloc/lib/src/replay_bloc.dart @@ -94,42 +94,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();