Comments (2)
import 'dart:async';
import 'package:bloc/bloc.dart';
void main() async{
final pushNotification = PushNotification();
final v1 = V1PushNotificationBloc(pushNotification: pushNotification);
v1.add(PushNotificationEvent.started());
v1.add(PushNotificationEvent.subscribe("V1PushNotificationBlocTopic"));
await Future.delayed(Duration(seconds: 1), (){
print("\n\n\n");
});
final v2 = V2PushNotificationBloc(pushNotification: pushNotification);
v2.add(PushNotificationEvent.started());
v2.add(PushNotificationEvent.subscribe("V2PushNotificationBlocTopic"));
}
class PushNotification {
Future<String> initNotification() async {
await Future.delayed(Duration(milliseconds: 200));
return "token";
}
Future<void> subscribeToTopic(String topic) async {
await Future.delayed(Duration(milliseconds: 200));
print("PushNotification.subscribeToTopic($topic) is called");
}
Future<void> unsubscribeFromTopic(String topic) async {
await Future.delayed(Duration(milliseconds: 200));
print("PushNotification.unsubscribeFromTopic($topic) is called");
}
}
EventTransformer<Event> sequential<Event>() {
return (events, mapper) => events.asyncExpand(mapper);
}
sealed class PushNotificationEvent {
const factory PushNotificationEvent.started() = _Started;
const factory PushNotificationEvent.subscribe(String topicType) = _Subscribe;
const factory PushNotificationEvent.unsubscribe(String topicType) =
_Unsubscribe;
}
class _Started implements PushNotificationEvent {
const _Started();
}
class _Subscribe implements PushNotificationEvent {
final String topicType;
const _Subscribe(this.topicType);
}
class _Unsubscribe implements PushNotificationEvent {
final String topicType;
const _Unsubscribe(this.topicType);
}
sealed class PushNotificationState {
const factory PushNotificationState.initial() = _Initial;
const factory PushNotificationState.granted({required String token}) =
_Granted;
const factory PushNotificationState.denied() = _Denied;
}
class _Initial implements PushNotificationState {
const _Initial();
}
class _Granted implements PushNotificationState {
final String token;
const _Granted({required this.token});
}
class _Denied implements PushNotificationState {
const _Denied();
}
class V1PushNotificationBloc
extends Bloc<PushNotificationEvent, PushNotificationState> {
V1PushNotificationBloc({required PushNotification pushNotification})
: _pushNotification = pushNotification,
super(const PushNotificationState.initial()) {
on<_Started>(_onStarted, transformer: sequential());
on<_Subscribe>(_onSubscribe, transformer: sequential());
on<_Unsubscribe>(_onUnsubscribe, transformer: sequential());
}
final PushNotification _pushNotification;
FutureOr<void> _onStarted(
_Started event, Emitter<PushNotificationState> emit) async {
print(
"V1PushNotificationBloc._onStarted is called-The current state is PushNotificationState.initial");
String token = await _pushNotification.initNotification();
emit(PushNotificationState.granted(token: token));
print(
"V1PushNotificationBloc._onStarted is called-The current state is PushNotificationState.granted");
}
FutureOr<void> _onSubscribe(
_Subscribe event, Emitter<PushNotificationState> emit) =>
switch (state) {
final _Granted grantedState => (_Granted grantedState) async {
await _pushNotification.subscribeToTopic(event.topicType);
print(
"V1PushNotificationBloc._onSubscribe is called-Topic type: ${event.topicType}-The current state is granted");
}(grantedState),
_ => () {
print(
"V1PushNotificationBloc._onSubscribe is called-Topic type: ${event.topicType}-The current state is initial");
}()
};
FutureOr<void> _onUnsubscribe(
_Unsubscribe event, Emitter<PushNotificationState> emit) =>
switch (state) {
final _Granted grantedState => (_Granted grantedState) async {
await _pushNotification.unsubscribeFromTopic(event.topicType);
print(
"V1PushNotificationBloc._onUnsubscribe is called-Topic type: ${event.topicType}-The current state is granted");
}(grantedState),
_ => () {
print(
"V1PushNotificationBloc._onUnsubscribe is called-Topic type: ${event.topicType}-The current state is initial");
}()
};
}
class V2PushNotificationBloc
extends Bloc<PushNotificationEvent, PushNotificationState> {
V2PushNotificationBloc({required PushNotification pushNotification})
: _pushNotification = pushNotification,
super(const PushNotificationState.initial()) {
on<PushNotificationEvent>(
(event, emit) => switch (event) {
_Started() => _onStarted(event, emit),
_Subscribe() => _onSubscribe(event, emit),
_Unsubscribe() => _onUnsubscribe(event, emit),
},
transformer: sequential());
}
final PushNotification _pushNotification;
FutureOr<void> _onStarted(
_Started event, Emitter<PushNotificationState> emit) async {
print(
"V2PushNotificationBloc._onStarted is called-The current state is PushNotificationState.initial");
String token = await _pushNotification.initNotification();
emit(PushNotificationState.granted(token: token));
print(
"V2PushNotificationBloc._onStarted is called-The current state is PushNotificationState.granted");
}
FutureOr<void> _onSubscribe(
_Subscribe event, Emitter<PushNotificationState> emit) =>
switch (state) {
final _Granted grantedState => (_Granted grantedState) async {
await _pushNotification.subscribeToTopic(event.topicType);
print(
"V2PushNotificationBloc._onSubscribe is called-Topic type: ${event.topicType}-The current state is granted");
}(grantedState),
_ => () {
print(
"V2PushNotificationBloc._onSubscribe is called-Topic type: ${event.topicType}-The current state is initial");
}()
};
FutureOr<void> _onUnsubscribe(
_Unsubscribe event, Emitter<PushNotificationState> emit) =>
switch (state) {
final _Granted grantedState => (_Granted grantedState) async {
await _pushNotification.unsubscribeFromTopic(event.topicType);
print(
"V2PushNotificationBloc._onUnsubscribe is called-Topic type: ${event.topicType}-The current state is granted");
}(grantedState),
_ => () {
print(
"V2PushNotificationBloc._onUnsubscribe is called-Topic type: ${event.topicType}-The current state is initial");
}()
};
}
/// Output:
// V1PushNotificationBloc._onStarted is called-The current state is PushNotificationState.initial
// V1PushNotificationBloc._onSubscribe is called-Topic type: V1PushNotificationBlocTopic-The current state is initial
// V1PushNotificationBloc._onStarted is called-The current state is PushNotificationState.granted
//
//
//
//
// V2PushNotificationBloc._onStarted is called-The current state is PushNotificationState.initial
// V2PushNotificationBloc._onStarted is called-The current state is PushNotificationState.granted
// PushNotification.subscribeToTopic(V2PushNotificationBlocTopic) is called
// V2PushNotificationBloc._onSubscribe is called-Topic type: V2PushNotificationBlocTopic-The current state is granted
from bloc.
Hi @II11II 👋
Thanks for opening an issue!
This is working as expected. Transformers only apply to the event handler that they are applied to. If you want all events to be sequential, then you need to create a single event handler like so:
sealed class CounterEvent {}
final class Increment extends CounterEvent {}
final class Decrement extends CounterEvent {}
class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0) {
on<CounterEvent>(
(event, emit) {
return switch (event) {
Increment() => emit(state + 1),
Decrement() => emit(state - 1),
};
},
transformer: sequential(),
);
}
}
Hope that helps! Closing for now 👍
from bloc.
Related Issues (20)
- fix: States of Different instances of the same bloc are interfering
- docs: BlocListener example loss a "," HOT 1
- chore: refactoring to `bloc` advices needed HOT 4
- fix: Bloc not calling while calling event with cascade in BlocProvider. But working fine if assigned to a variable and passed to BlocProvider. HOT 1
- fix: expected: [<States>], actual [] when unit testing landing page cubit HOT 4
- Bad state: Cannot emit new states after calling close HOT 2
- State not update when adding a new item to the list HOT 1
- question: Is it a good practice to use "business" bloc HOT 4
- docs: replay bloc with multi bloc HOT 2
- fix: MultiBlocProvider and easy_localization library problem HOT 2
- docs: Add zh-cn translation for Bloc Concepts page HOT 1
- feat: Make hydrated_storage.dart:Storage.read() method async
- BlocBuilder widget not rebuilding the ui on certain states. HOT 6
- question: How do I design the architecture of A very complex desktop app HOT 3
- feat(bloc)!: add support for strongly typed errors on `Emitter.onEach`/`Emitter.forEach` HOT 2
- feat(bloc): add support for optional `StackTrace` on error handlers for `Emitter.onEach`/`Emitter.forEach` HOT 2
- feat: Add `force` to Emitter HOT 7
- feat: Privacy manifest file for iOS is missing HOT 4
- question: BlocObserver and Injectable Singletons HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bloc.