Propagation of change

PHOTO EMBED

Mon Dec 26 2022 23:12:54 GMT+0000 (Coordinated Universal Time)

Saved by @ilivanilton #angular #rxjs-pattern

import { combineLatest, Subject, map } from 'rxjs';

var price = 31.99;

/** Declare an Observable
 * Data we can observe
 * over time
 */
const qty$ = new Subject<number>();

/** Emit when the action occurs
 * Hey Observable,
 * here is a new value
 */
const onQuantityChanged = (quantity) => qty$.next(quantity);

/** React to emissions
 * Whenever you
 * change, I`II change
 */
const exPreice$ = qty$.pipe(map((q) => q * price));
const tax$ = exPreice$.pipe(map((t) => Math.round(t * 10.75) / 100));
const deliveryFree$ = exPreice$.pipe(map((q) => (q < 35 ? 5.99 : 0)));
const totalPrice$ = combineLatest([exPreice$, deliveryFree$, tax$]).pipe(
  map(([p, d, t]) => p + d + t)
);
totalPrice$.subscribe(console.log);
onQuantityChanged(2); // 70.86
content_copyCOPY

https://youtu.be/qxjt5vlG58s?t=2309