Skip to content

Instantly share code, notes, and snippets.

@rullyalves
Created August 23, 2019 18:25
Show Gist options
  • Select an option

  • Save rullyalves/f70ed5c481bbaef58020412199b55d4b to your computer and use it in GitHub Desktop.

Select an option

Save rullyalves/f70ed5c481bbaef58020412199b55d4b to your computer and use it in GitHub Desktop.
class CartBloc extends BlocBase {
Observable<double> total;
Observable<List<CartItem>> itens;
Observable<ShoppingCart> _cartBase;
set add(Product i) => _add.sink.add(i);
set delete(CartItem i) => _remove.sink.add(i);
final _add = BehaviorSubject<Product>();
final _cart = BehaviorSubject<ShoppingCart>.seeded(ShoppingCart());
final _remove = BehaviorSubject<CartItem>();
CartBloc() {
final add = Observable.combineLatest2<Product, ShoppingCart, ShoppingCart>(
_add, _cart, (a, b) => b..add(a));
final remove =
Observable.combineLatest2<CartItem, ShoppingCart, ShoppingCart>(
_remove, _cart, (a, b) => b..remove(a));
_cartBase = add
.mergeWith([remove])
.publishValue()
.autoConnect();
itens = _cartBase
.map((e) => e.items);
total = _cartBase
.map((e) => e.total);
}
void dispose() {
super.dispose();
_add.close();
_remove.close();
_cart.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment