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(bloc): deprecate BlocOverrides #3469

Merged
merged 4 commits into from Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 4 additions & 12 deletions packages/bloc/README.md
Expand Up @@ -172,12 +172,8 @@ class MyBlocObserver extends BlocObserver {

```dart
void main() {
BlocOverrides.runZoned(
() {
// Use cubits...
},
blocObserver: MyBlocObserver(),
);
Bloc.observer = MyBlocObserver();
// Use cubits...
}
```

Expand Down Expand Up @@ -327,12 +323,8 @@ class MyBlocObserver extends BlocObserver {

```dart
void main() {
BlocOverrides.runZoned(
() {
// Use blocs...
},
blocObserver: MyBlocObserver(),
);
Bloc.observer = MyBlocObserver();
// Use blocs...
}
```

Expand Down
7 changes: 3 additions & 4 deletions packages/bloc/example/main.dart
Expand Up @@ -41,10 +41,9 @@ class SimpleBlocObserver extends BlocObserver {
}

void main() {
BlocOverrides.runZoned(() {
cubitMain();
blocMain();
}, blocObserver: SimpleBlocObserver());
Bloc.observer = SimpleBlocObserver();
cubitMain();
blocMain();
}

void cubitMain() {
Expand Down
25 changes: 22 additions & 3 deletions packages/bloc/lib/src/bloc.dart
Expand Up @@ -42,6 +42,8 @@ class _Handler {
final Type type;
}

class _DefaultBlocObserver extends BlocObserver {}

/// {@template bloc}
/// Takes a `Stream` of `Events` as input
/// and transforms them into a `Stream` of `States` as output.
Expand All @@ -51,12 +53,29 @@ abstract class Bloc<Event, State> extends BlocBase<State>
/// {@macro bloc}
Bloc(State initialState) : super(initialState);

/// The current [BlocObserver] instance.
static BlocObserver observer = _DefaultBlocObserver();

/// The default [EventTransformer] used for all event handlers.
/// By default all events are processed concurrently.
///
/// If a custom transformer is specified for a particular event handler,
/// it will take precendence over the global transformer.
///
/// See also:
///
/// * [package:bloc_concurrency](https://pub.dev/packages/bloc_concurrency) for an
/// opinionated set of event transformers.
///
static EventTransformer<dynamic> transformer = _defaultEventTransformer;

final _eventController = StreamController<Event>.broadcast();
final _subscriptions = <StreamSubscription<dynamic>>[];
final _handlers = <_Handler>[];
final _emitters = <_Emitter>[];
final _eventTransformer =
BlocOverrides.current?.eventTransformer ?? _defaultEventTransformer;
// ignore: deprecated_member_use_from_same_package
BlocOverrides.current?.eventTransformer ?? Bloc.transformer;

/// Notifies the [Bloc] of a new [event] which triggers
/// all corresponding [EventHandler] instances.
Expand Down Expand Up @@ -110,7 +129,7 @@ abstract class Bloc<Event, State> extends BlocBase<State>
@mustCallSuper
void onEvent(Event event) {
// ignore: invalid_use_of_protected_member
_blocObserver?.onEvent(this, event);
_blocObserver.onEvent(this, event);
}

/// {@template emit}
Expand Down Expand Up @@ -250,7 +269,7 @@ abstract class Bloc<Event, State> extends BlocBase<State>
@mustCallSuper
void onTransition(Transition<Event, State> transition) {
// ignore: invalid_use_of_protected_member
_blocObserver?.onTransition(this, transition);
_blocObserver.onTransition(this, transition);
}

/// Closes the `event` and `state` `Streams`.
Expand Down
11 changes: 6 additions & 5 deletions packages/bloc/lib/src/bloc_base.dart
Expand Up @@ -53,10 +53,11 @@ abstract class BlocBase<State>
/// {@macro bloc_base}
BlocBase(this._state) {
// ignore: invalid_use_of_protected_member
_blocObserver?.onCreate(this);
_blocObserver.onCreate(this);
}

final _blocObserver = BlocOverrides.current?.blocObserver;
// ignore: deprecated_member_use_from_same_package
final _blocObserver = BlocOverrides.current?.blocObserver ?? Bloc.observer;

late final _stateController = StreamController<State>.broadcast();

Expand Down Expand Up @@ -129,7 +130,7 @@ abstract class BlocBase<State>
@mustCallSuper
void onChange(Change<State> change) {
// ignore: invalid_use_of_protected_member
_blocObserver?.onChange(this, change);
_blocObserver.onChange(this, change);
}

/// Reports an [error] which triggers [onError] with an optional [StackTrace].
Expand Down Expand Up @@ -157,7 +158,7 @@ abstract class BlocBase<State>
@mustCallSuper
void onError(Object error, StackTrace stackTrace) {
// ignore: invalid_use_of_protected_member
_blocObserver?.onError(this, error, stackTrace);
_blocObserver.onError(this, error, stackTrace);
}

/// Closes the instance.
Expand All @@ -167,7 +168,7 @@ abstract class BlocBase<State>
@override
Future<void> close() async {
// ignore: invalid_use_of_protected_member
_blocObserver?.onClose(this);
_blocObserver.onClose(this);
await _stateController.close();
}
}
14 changes: 12 additions & 2 deletions packages/bloc/lib/src/bloc_overrides.dart
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use_from_same_package

part of 'bloc.dart';

const _asyncRunZoned = runZoned;
Expand Down Expand Up @@ -34,9 +36,13 @@ abstract class BlocOverrides {
/// See also:
/// * [BlocOverrides.runZoned] to provide [BlocOverrides] in a fresh [Zone].
///
@Deprecated('This will be removed in bloc v9.0.0.')
static BlocOverrides? get current => Zone.current[_token] as BlocOverrides?;

/// Runs [body] in a fresh [Zone] using the provided overrides.
@Deprecated(
'This will be removed in bloc v9.0.0. Please use Bloc.observer/Bloc.transformer instead.',
)
static R runZoned<R>(
R Function() body, {
BlocObserver? blocObserver,
Expand All @@ -49,6 +55,9 @@ abstract class BlocOverrides {
/// The [BlocObserver] that will be used within the current [Zone].
///
/// By default, a base [BlocObserver] implementation is used.
@Deprecated(
'This will be removed in bloc v9.0.0. Please use Bloc.observer instead.',
)
BlocObserver get blocObserver => _defaultBlocObserver;

/// The [EventTransformer] that will be used within the current [Zone].
Expand All @@ -63,6 +72,9 @@ abstract class BlocOverrides {
/// * [package:bloc_concurrency](https://pub.dev/packages/bloc_concurrency) for an
/// opinionated set of event transformers.
///
@Deprecated(
'This will be removed in bloc v9.0.0. Please use Bloc.transformer instead.',
)
EventTransformer get eventTransformer => _defaultEventTransformer;
}

Expand Down Expand Up @@ -103,8 +115,6 @@ late final _defaultEventTransformer = (Stream events, EventMapper mapper) {
.transform<dynamic>(const _FlatMapStreamTransformer<dynamic>());
};

class _DefaultBlocObserver extends BlocObserver {}

class _FlatMapStreamTransformer<T> extends StreamTransformerBase<Stream<T>, T> {
const _FlatMapStreamTransformer();

Expand Down
122 changes: 64 additions & 58 deletions packages/bloc/test/bloc_event_transformer_test.dart
Expand Up @@ -39,6 +39,16 @@ class CounterBloc extends Bloc<CounterEvent, int> {
}

void main() {
late EventTransformer transformer;

setUp(() {
transformer = Bloc.transformer;
});

tearDown(() {
Bloc.transformer = transformer;
});

test('processes events concurrently by default', () async {
final states = <int>[];
final bloc = CounterBloc()
Expand Down Expand Up @@ -186,81 +196,77 @@ void main() {
test(
'processes events sequentially when '
'Bloc.transformer is overridden.', () async {
await BlocOverrides.runZoned(
() async {
final states = <int>[];
final bloc = CounterBloc()
..stream.listen(states.add)
..add(Increment())
..add(Increment())
..add(Increment());

await tick();
Bloc.transformer = (events, mapper) => events.asyncExpand<dynamic>(mapper);
final states = <int>[];
final bloc = CounterBloc()
..stream.listen(states.add)
..add(Increment())
..add(Increment())
..add(Increment());

expect(
bloc.onCalls,
equals([Increment()]),
);
await tick();

await wait();
expect(
bloc.onCalls,
equals([Increment()]),
);

expect(
bloc.onEmitCalls,
equals([Increment()]),
);
expect(states, equals([1]));
await wait();

await tick();
expect(
bloc.onEmitCalls,
equals([Increment()]),
);
expect(states, equals([1]));

expect(
bloc.onCalls,
equals([Increment(), Increment()]),
);
await tick();

await wait();
expect(
bloc.onCalls,
equals([Increment(), Increment()]),
);

expect(
bloc.onEmitCalls,
equals([Increment(), Increment()]),
);
await wait();

expect(states, equals([1, 2]));
expect(
bloc.onEmitCalls,
equals([Increment(), Increment()]),
);

await tick();
expect(states, equals([1, 2]));

expect(
bloc.onCalls,
equals([
Increment(),
Increment(),
Increment(),
]),
);
await tick();

await wait();
expect(
bloc.onCalls,
equals([
Increment(),
Increment(),
Increment(),
]),
);

expect(
bloc.onEmitCalls,
equals([Increment(), Increment(), Increment()]),
);
await wait();

expect(states, equals([1, 2, 3]));
expect(
bloc.onEmitCalls,
equals([Increment(), Increment(), Increment()]),
);

await bloc.close();
expect(states, equals([1, 2, 3]));

expect(
bloc.onCalls,
equals([Increment(), Increment(), Increment()]),
);
await bloc.close();

expect(
bloc.onEmitCalls,
equals([Increment(), Increment(), Increment()]),
);
expect(
bloc.onCalls,
equals([Increment(), Increment(), Increment()]),
);

expect(states, equals([1, 2, 3]));
},
eventTransformer: (events, mapper) => events.asyncExpand<dynamic>(mapper),
expect(
bloc.onEmitCalls,
equals([Increment(), Increment(), Increment()]),
);

expect(states, equals([1, 2, 3]));
});
}