Skip to content

Commit

Permalink
chore: replaced event emitters with subjects
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmihi committed Dec 10, 2023
1 parent c1a9ed7 commit 700b414
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/app/wish-list/wish-list.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Component, OnInit } from '@angular/core';
import {Component, OnDestroy, OnInit} from '@angular/core';

import { Wish } from '../shared/wish.model';
import { WishEditComponent } from './wish-edit/wish-edit.component';
import { CommonModule } from '@angular/common';
import { CardModule } from 'primeng/card';
import {wishListService} from "./wishList.service";
import {Subscription} from "rxjs";

@Component({
standalone: true,
Expand All @@ -13,13 +14,17 @@ import {wishListService} from "./wishList.service";
imports: [WishEditComponent, CommonModule, CardModule],
styleUrls: ['./wish-list.component.css'],
})
export class WishListComponent implements OnInit {
export class WishListComponent implements OnInit, OnDestroy {
wishes: Wish[] | undefined;

private wishChanged: Subscription | undefined;
constructor(private WishListService: wishListService) {}

ngOnInit() {
this.wishes = this.WishListService.getWishList()
this.WishListService.wishesChanged.subscribe((wishes:Wish[])=>{this.wishes = wishes})
this.wishChanged = this.WishListService.wishesChanged.subscribe((wishes:Wish[])=>{this.wishes = wishes})
}

ngOnDestroy() {
this.wishChanged?.unsubscribe();
}
}
6 changes: 3 additions & 3 deletions src/app/wish-list/wishList.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Wish} from "../shared/wish.model";
import {EventEmitter} from "@angular/core";
import {Subject} from "rxjs";

export class wishListService {
wishesChanged = new EventEmitter<Wish[]>();
wishesChanged = new Subject<Wish[]>();
private wishes: Wish[] = [
new Wish('Apples Are Tasty', 5),
new Wish('Tomatoes Rot', 10),
Expand All @@ -14,6 +14,6 @@ export class wishListService {

addWishList(wish: Wish) {
this.wishes.push(wish);
this.wishesChanged.emit(this.wishes.slice());
this.wishesChanged.next(this.wishes.slice());
}
}

0 comments on commit 700b414

Please sign in to comment.