Skip to content
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

feat: #3555 #3933

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/bloc/lib/src/bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ abstract class Bloc<Event, State> extends BlocBase<State>
/// {@endtemplate}
@visibleForTesting
@override
void emit(State state) => super.emit(state);
Emitter<State> 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`.
Expand Down
36 changes: 18 additions & 18 deletions packages/bloc/lib/src/bloc_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<State extends Object?> {
/// Emits a new [state].
void emit(State state);
/// The emitter.
Emitter<State> get emit;
}

/// A generic destination for errors.
Expand Down Expand Up @@ -90,21 +90,21 @@ abstract class BlocBase<State>
@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<State>(currentState: this.state, nextState: state));
_state = state;
_stateController.add(_state);
_emitted = true;
} catch (error, stackTrace) {
onError(error, stackTrace);
rethrow;
}
}
Emitter<State> get emit => _Emitter((state) {
try {
if (isClosed) {
throw StateError('Cannot emit new states after calling close');
}
if (state == _state && _emitted) return;
onChange(Change<State>(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.
Expand Down
3 changes: 3 additions & 0 deletions packages/bloc/lib/src/emitter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
///
/// {@endtemplate}
abstract class Emitter<State> {
/// {@macro emitter}
factory Emitter(void Function(State state) emit) => _Emitter(emit);

Check warning on line 13 in packages/bloc/lib/src/emitter.dart

View check run for this annotation

Codecov / codecov/patch

packages/bloc/lib/src/emitter.dart#L13

Added line #L13 was not covered by tests

/// Subscribes to the provided [stream] and invokes the [onData] callback
/// when the [stream] emits new data.
///
Expand Down
72 changes: 36 additions & 36 deletions packages/replay_bloc/lib/src/replay_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,42 +94,42 @@ mixin ReplayBlocMixin<Event extends ReplayEvent, State> on Bloc<Event, State> {
}

@override
void emit(State state) {
_changeStack.add(
_Change<State>(
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<State> get emit => Emitter((state) {
_changeStack.add(
_Change<State>(
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();
Expand Down
22 changes: 11 additions & 11 deletions packages/replay_bloc/lib/src/replay_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ mixin ReplayCubitMixin<State> on Cubit<State> {
set limit(int limit) => _changeStack.limit = limit;

@override
void emit(State state) {
_changeStack.add(
_Change<State>(
this.state,
state,
() => super.emit(state),
(val) => super.emit(val),
),
);
super.emit(state);
}
Emitter<State> get emit => Emitter((state) {
_changeStack.add(
_Change<State>(
this.state,
state,
() => super.emit(state),
(val) => super.emit(val),
),
);
super.emit(state);
});

/// Undo the last change.
void undo() => _changeStack.undo();
Expand Down
Loading