-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move bloc related APIs to comms package (#60)
* Move bloc related APIs to comms package - update changelog and pubspec version - add bloc package - add ListenerBloc, ListenerCubit and StateSender - add tests - extract ProductCountChangedMessage to seperate file Related to #59 * Update README
- Loading branch information
1 parent
e5002d8
commit 0049c21
Showing
13 changed files
with
291 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
library comms; | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:bloc/bloc.dart'; | ||
import 'package:logging/logging.dart'; | ||
import 'package:meta/meta.dart' show nonVirtual, protected, visibleForTesting; | ||
import 'package:meta/meta.dart' | ||
show mustCallSuper, nonVirtual, protected, visibleForTesting; | ||
|
||
part 'src/listener.dart'; | ||
part 'src/message_sink_register.dart'; | ||
part 'src/sender.dart'; | ||
part 'src/multi_listener.dart'; | ||
part 'src/bloc/listener_bloc.dart'; | ||
part 'src/bloc/listener_cubit.dart'; | ||
part 'src/bloc/state_sender.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
part of '../../comms.dart'; | ||
|
||
/// Handles [Listener]'s [listen] and [close]. | ||
/// | ||
/// If your bloc needs to extend another class use [Listener] mixin. | ||
/// | ||
/// Type argument [Message] marks what type of messages can be received. | ||
/// | ||
/// See also: | ||
/// | ||
/// * ListenerCubit, a class extending [Cubit] which handles calling [listen] | ||
/// and [cancel] automatically. | ||
/// * useMessageListener, a hook that provides [Listener]'s functionality | ||
/// which handles calling [listen] and [cancel] automatically. | ||
abstract class ListenerBloc<Event, State, Message> extends Bloc<Event, State> | ||
with Listener<Message> { | ||
ListenerBloc(super.initialState) { | ||
super.listen(); | ||
} | ||
|
||
@override | ||
@mustCallSuper | ||
Future<void> close() { | ||
super.cancel(); | ||
return super.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
part of '../../comms.dart'; | ||
|
||
/// Handles [Listener]'s [listen] and [close]. | ||
/// | ||
/// Use instead of [Cubit]. | ||
/// | ||
/// If your cubit needs to extend another class use [Listener] mixin. | ||
/// | ||
/// Type argument [Message] marks what type of messages can be received. | ||
/// | ||
/// See also: | ||
/// | ||
/// * ListenerBloc, a class extending [Bloc] which handles calling [listen] | ||
/// and [cancel] automatically. | ||
/// * useMessageListener, a hook that provides [Listener]'s functionality | ||
/// which handles calling [listen] and [cancel] automatically. | ||
abstract class ListenerCubit<State, Message> extends Cubit<State> | ||
with Listener<Message> { | ||
ListenerCubit(super.initialState) { | ||
super.listen(); | ||
} | ||
|
||
@override | ||
@mustCallSuper | ||
Future<void> close() { | ||
super.cancel(); | ||
return super.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
part of '../../comms.dart'; | ||
|
||
/// Sends emitted [State]s to all [Listener]s of type [State]. | ||
mixin StateSender<State> on BlocBase<State> { | ||
@override | ||
@mustCallSuper | ||
void onChange(Change<State> change) { | ||
super.onChange(change); | ||
getSend<State>()(change.nextState); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'package:comms/comms.dart'; | ||
|
||
import '../messages/product_count_changed.dart'; | ||
|
||
class ProductCountListenerCubit | ||
extends ListenerCubit<int, ProductCountChangedMessage> { | ||
ProductCountListenerCubit() : super(0); | ||
|
||
@override | ||
void onMessage(ProductCountChangedMessage message) { | ||
if (message is ProductCountIncremented) { | ||
_increment(); | ||
} | ||
if (message is ProductCountDecremented) { | ||
_decrement(); | ||
} | ||
} | ||
|
||
@override | ||
void onInitialMessage(ProductCountChangedMessage message) => | ||
onMessage(message); | ||
|
||
void _increment() { | ||
emit(state + 1); | ||
} | ||
|
||
void _decrement() { | ||
emit(state - 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class ProductCountChangedMessage {} | ||
|
||
class ProductCountIncremented extends ProductCountChangedMessage {} | ||
|
||
class ProductCountDecremented extends ProductCountChangedMessage {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:comms/comms.dart'; | ||
import '../messages/product_count_changed.dart'; | ||
|
||
class BasketCubit extends Cubit<List<String>> | ||
with Sender<ProductCountChangedMessage> { | ||
BasketCubit() : super([]); | ||
|
||
void add(String product) { | ||
emit([...state, product]); | ||
send(ProductCountIncremented()); | ||
} | ||
|
||
void removeLast() { | ||
emit([...state]..removeLast()); | ||
send(ProductCountDecremented()); | ||
} | ||
} |