Permalink
Browse files

Auto-generated commit -- Introduce lettable RxJS operators

- use the Observable.pipe() method
- imports each operator as named import from rxjs/operators
- rename do() and catch() (see the table in our blog article)

(cherry picked from commit 318c9a444b51a4cb9f37eebc8cf1fb6aae74a5eb)

----

Remove unused imports

(cherry picked from commit 1e1f4a41f7234baf045f2fab71c0216fa145ad11)

----

Use lettable operators in app component

(cherry picked from commit c48403f3602ef9e09be5a9a211a1bc18f7ad115a)
Signed-off-by: Johannes Hoppe <johannes.hoppe@haushoppe-its.de>

Triggered by commit: angular-buch/book-monkey2@556c596
Travis build: https://travis-ci.org/angular-buch/book-monkey2/builds/307864460
  • Loading branch information...
TheBuildbot committed Nov 27, 2017
1 parent efd8839 commit 043a3d0bb509aea3b6d714fdda75f55d7283c842
Showing with 46 additions and 34 deletions.
  1. +5 −3 src/app/admin/shared/book.validators.ts
  2. +9 −11 src/app/search/search.component.ts
  3. +32 −20 src/app/shared/book-store.service.ts
@@ -1,5 +1,6 @@
import { FormControl, FormArray } from '@angular/forms';
import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators';

import { BookStoreService } from '../../shared/book-store.service';

@@ -26,10 +27,11 @@ export class BookValidators {

static isbnExists(bs: BookStoreService) {
return function(control: FormControl): Observable<{ [error: string]: any }> {
return bs.check(control.value)
.map(exists => (exists === false) ? null : {
return bs.check(control.value).pipe(
map(exists => (exists === false) ? null : {
isbnExists: { valid: false }
});
})
);
};
}
}
@@ -1,8 +1,5 @@
import { Component, EventEmitter, Output, OnInit } from '@angular/core';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/switchMap';
import { debounceTime, distinctUntilChanged, tap, switchMap } from 'rxjs/operators';

import { Book } from '../shared/book';
import { BookStoreService } from '../shared/book-store.service';
@@ -23,12 +20,13 @@ export class SearchComponent implements OnInit {

ngOnInit() {

this.keyup
.debounceTime(500)
.distinctUntilChanged()
.do(() => this.isLoading = true)
.switchMap(searchTerm => this.bs.getAllSearch(searchTerm))
.do(() => this.isLoading = false)
.subscribe(books => this.foundBooks = books);
this.keyup.pipe(
debounceTime(500),
distinctUntilChanged(),
tap(() => this.isLoading = true),
switchMap(searchTerm => this.bs.getAllSearch(searchTerm)),
tap(() => this.isLoading = false)
)
.subscribe(books => this.foundBooks = books);
}
}
@@ -1,9 +1,7 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/retry';
import 'rxjs/add/operator/catch';
import { retry, map, catchError } from 'rxjs/operators';
import 'rxjs/add/observable/throw';

import { Book } from './book';
@@ -18,43 +16,55 @@ export class BookStoreService {
getAll(): Observable<Array<Book>> {
return this.http
.get<any[]>(`${this.api}/books`)
.retry(3)
.map(rawBooks => rawBooks
.map(rawBook => BookFactory.fromObject(rawBook))
)
.catch(this.errorHandler);
.pipe(
retry(3),
map(rawBooks => rawBooks
.map(rawBook => BookFactory.fromObject(rawBook)),
),
catchError(this.errorHandler)
);
}

getSingle(isbn: string): Observable<Book> {
return this.http
.get(`${this.api}/book/${isbn}`)
.retry(3)
.map(rawBook => BookFactory.fromObject(rawBook))
.catch(this.errorHandler);
.pipe(
retry(3),
map(rawBook => BookFactory.fromObject(rawBook)),
catchError(this.errorHandler)
);
}

check(isbn: string): Observable<Boolean> {
return this.http
.get(`${this.api}/book/${isbn}/check`)
.catch(this.errorHandler);
.pipe(
catchError(this.errorHandler)
);
}

create(book: Book): Observable<any> {
return this.http
.post(`${this.api}/book`, book, { responseType: 'text' })
.catch(this.errorHandler);
.pipe(
catchError(this.errorHandler)
);
}

update(book: Book): Observable<any> {
return this.http
.put(`${this.api}/book/${book.isbn}`, book, { responseType: 'text' })
.catch(this.errorHandler);
.pipe(
catchError(this.errorHandler)
);
}

remove(isbn: string): Observable<any> {
return this.http
.delete(`${this.api}/book/${isbn}`, { responseType: 'text' })
.catch(this.errorHandler);
.pipe(
catchError(this.errorHandler)
);
}

private errorHandler(error: Error | any): Observable<any> {
@@ -64,10 +74,12 @@ export class BookStoreService {
getAllSearch(searchTerm: string): Observable<Array<Book>> {
return this.http
.get<any[]>(`${this.api}/books/search/${searchTerm}`)
.retry(3)
.map(rawBooks => rawBooks
.map(rawBook => BookFactory.fromObject(rawBook))
)
.catch(this.errorHandler);
.pipe(
retry(3),
map(rawBooks => rawBooks
.map(rawBook => BookFactory.fromObject(rawBook)),
),
catchError(this.errorHandler)
);
}
}

0 comments on commit 043a3d0

Please sign in to comment.