PillowPillow / ng2-webstorage

Localstorage and sessionstorage manager - Angular service
MIT License
426 stars 92 forks source link

Not triggering when splicing #140

Open mackelito opened 3 years ago

mackelito commented 3 years ago

Not sure if it's just me or if this is a bug or not :D

  addToCart(cartArticle: CartArticle) {
    this.cart.articles.push(cartArticle);
    this.refreshExpireDate();
    this._storage.store('cart', this.cart);
  }

  removeFromCart(index: number) {
    this.cart.articles.splice(index, 1);
    this.refreshExpireDate();
    this._storage.store('cart', this.cart);
  }

In both functions I see that the refreshExpireDate does trigger in my service as it is updated in localstorage...

but the subscription I have does not trigger on remove.. only add?

    this.storage.observe('cart').pipe(
      startWith(this.storage.retrieve('cart'))
    ).subscribe(cart => {
      console.log('checkoutpage', cart);
      this.cartSubject.next(cart);
    });
mackelito commented 3 years ago

Oh.. It must be something with the dirty checking in angular where the object does not change but only the underlaying array..

mackelito commented 3 years ago

Spoke to soon.. There is something else going on here..

  ngOnInit() {
    this.storage.observe('cart').pipe(
      startWith(this.storage.retrieve('cart'))
    ).subscribe(cart => {
      console.log('summary', cart);
      this.cartSubject.next(cart);
    });
  }

This summary only triggers on the first remove.. but all adds?