Skip to content

Instantly share code, notes, and snippets.

@seysdev
Last active May 22, 2019 17:20
Show Gist options
  • Select an option

  • Save seysdev/5ae856f0d7967bdcb62260cf4c973402 to your computer and use it in GitHub Desktop.

Select an option

Save seysdev/5ae856f0d7967bdcb62260cf4c973402 to your computer and use it in GitHub Desktop.

Revisions

  1. @blessebas blessebas revised this gist May 22, 2019. 1 changed file with 50 additions and 2 deletions.
    52 changes: 50 additions & 2 deletions reactive-ex1.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    /*
    * Without reactive paradigm
    */
    class Car {
    constructor(sales) {
    this.items = [];
    @@ -12,7 +15,6 @@ class Car {
    const total = this.items.reduce(
    (vant, vact) => vant + vact.price, 0
    );

    this.sales.update(total.toFixed(3));
    }
    }
    @@ -34,4 +36,50 @@ car.add({ name: 'Ford', price: 14.200 });
    car.add({ name: 'Toyota', price: 10.000 });
    car.checkout();

    console.log('sales',sales.total); // 24.200
    console.log('sales',sales.total); // 24.200


    /*
    * With reactive paradigm
    */

    import { Subject } from 'rxjs';
    class Car {
    constructor() {
    this.items = [];
    this.total = [];
    this.cObs = new Subject()
    }

    add(item) {
    this.items.push(item);
    }

    checkout() {
    const total = this.items.reduce(
    (vant, vact) => vant + vact.price, 0
    );

    this.cObs.next(total)
    }
    }

    class Sales {
    constructor(car) {
    this.total = 0;
    car.cObs.subscribe(this.update.bind(this))
    }

    update(total) {
    this.total = total;
    }
    }

    const car = new Car();
    const sales = new Sales(car);

    car.add({ name: 'Ford', price: 14.200 });
    car.add({ name: 'Toyota', price: 10.000 });
    car.checkout();

    console.log('sales', sales.total); // 24.200
  2. @blessebas blessebas renamed this gist May 22, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @blessebas blessebas created this gist May 22, 2019.
    37 changes: 37 additions & 0 deletions reactive-ex1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    class Car {
    constructor(sales) {
    this.items = [];
    this.sales = sales;
    }

    add(item) {
    this.items.push(item);
    }

    checkout() {
    const total = this.items.reduce(
    (vant, vact) => vant + vact.price, 0
    );

    this.sales.update(total.toFixed(3));
    }
    }

    class Sales {
    constructor() {
    this.total = 0;
    }

    update(total) {
    this.total = total;
    }
    }

    const sales = new Sales();
    const car = new Car(sales);

    car.add({ name: 'Ford', price: 14.200 });
    car.add({ name: 'Toyota', price: 10.000 });
    car.checkout();

    console.log('sales',sales.total); // 24.200