So I've been using Bloc pattern a lot in our app (about 20 bloc classes with multiple sinks and streams in each), really enjoying the streams part of it, combining and transforming is really powerful, but I'm struggling with the sinks.
Using them make sense but it makes writing code and tests a bit difficult, so I've been exposing functions instead of sinks and I'm wondering what kind of problems could this bring that I'm not noticing right now?
So to illustrate, instead of doing this:
class Bloc {
final _name = BehaviorSubject<String>();
Bloc() {
_name.listen(_handleName);
}
Sink<String> get addName => _name.sink.add;
void _handleName(String name) async {
await dosomethingwithname;
}
}
class BlocTest {
test('sink', () {
final bloc = Bloc();
bloc.addName.add('name');
//How to test or wait until it has processed??
});
}I do:
class Bloc {
final _name = BehaviorSubject<String>();
void addName(String name) async {
await dosomethingwithname;
}
}
class BlocTest {
test('sink', () {
final bloc = Bloc();
await bloc.addName('name');
//addName finished!
});
}Easy to test, more readable (no .add method call), really easy to react to the results, but not really Bloc pattern since blocs can only expose Sinks/Streams. Any reason I should stop doing this?