Created
August 23, 2019 18:25
-
-
Save rullyalves/f70ed5c481bbaef58020412199b55d4b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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