Open enyo opened 6 years ago
The ProductSquareBloc has its own StreamController for the cartItems. In its constructor it sets up a listener to that stream, to be able to update the isInCard stream:
ProductSquareBloc
StreamController
cartItems
isInCard
ProductSquareBloc(Product product) { _cartItemsController.stream .map((list) => list.any((item) => item.product == product)) .listen((isInCart) => _isInCartSubject.add(isInCart)); }
then the ProductSquare feeds that _cartItemsController in the _createBloc method:
ProductSquare
_cartItemsController
_createBloc
_bloc = ProductSquareBloc(widget.product); _subscription = widget.itemsStream.listen(_bloc.cartItems.add);
Why doesn't the ProductSquareBloc simply get the widget.itemsStream directly?
widget.itemsStream
It could be like this:
/// Creating the bloc _bloc = ProductSquareBloc(widget.product, onItems: widget.itemsStream);
and inside the bloc's constructor:
ProductSquareBloc(Product product, {Stream onItems}) { onItems .map((list) => list.any((item) => item.product == product)) .listen((isInCart) => _isInCartSubject.add(isInCart)); }
What is the advantage of managing it's own controller and stream like this?
The
ProductSquareBloc
has its ownStreamController
for thecartItems
. In its constructor it sets up a listener to that stream, to be able to update theisInCard
stream:then the
ProductSquare
feeds that_cartItemsController
in the_createBloc
method:My question is this:
Why doesn't the
ProductSquareBloc
simply get thewidget.itemsStream
directly?It could be like this:
and inside the bloc's constructor:
What is the advantage of managing it's own controller and stream like this?