我在效果中有一个if
控制语句,用于在从服务检索数据的情况下返回操作,如果没有返回另一个操作以从另一个Web API
获取数据。这是一个链式效果操作,其中有另一个效果来处理LOADFROMWEBAPI
操作。
是否有更好的方法并避免使用if
控制语句,并仅返回LoadFromWebAPI
之类的操作?可以返回动作SearchCompleteAction
的是问题 - 实际还是减速器?
@Effect()
search$: Observable<Action> = this.actions$
.ofType(book.SEARCH)
.debounceTime(300)
.map(toPayload)
.switchMap(query => {
if (query === '') {
return empty();
}
const nextSearch$ = this.actions$.ofType(book.SEARCH).skip(1);
return this.googleBooks.searchBooks(query)
.takeUntil(nextSearch$)
.map(books => {
if (data === undefined) {
return new book.LoadFromWebAPI(query);
} else {
return new book.SearchCompleteAction(books);
}
})
.catch(() => of(new book.SearchCompleteAction([])));
});
答案 0 :(得分:0)
@Effect()
search$: Observable<Action> = this.actions$
.ofType(book.SEARCH)
.debounceTime(300)
.map(toPayload)
.filter(query => query !== '')
.switchMap(query => {
const nextSearch$ = this.actions$.ofType(book.SEARCH).skip(1);
return this.googleBooks.searchBooks(query)
.takeUntil(nextSearch$)
.map(books => data === undefined ? new book.LoadFromWebAPI(query) : new book.SearchCompleteAction(books))
.catch(() => of(new book.SearchCompleteAction([])));
});