Skip to content

Instantly share code, notes, and snippets.

@aRmanNM
Created April 5, 2021 07:23
Show Gist options
  • Save aRmanNM/90aa6d5fb253244d0d6d95183d4ea2ca to your computer and use it in GitHub Desktop.
Save aRmanNM/90aa6d5fb253244d0d6d95183d4ea2ca to your computer and use it in GitHub Desktop.
Observable Data Service
@Injectable({
providedIn: 'root'
})
export class BookStoreService {
private _books = new BehaviorSubject<Book[]>([]);
public readonly books$: Observable<Book[]> = this._books.asObservable();
constructor(private bookBackend: BookBackendService) {
this.getBooks();
}
addBook(newBook: Book): void {
this.bookBackend.addBook(newBook).subscribe((res) => {
this._books.next(res);
});
}
getBooks(): void {
this.bookBackend.getBooks().subscribe((res) => {
this._books.next(res);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment