SwitchMap vs MergeMap dans le #ngrx exemple

Voici le code à partir de la Ngrx exemple: https://github.com/ngrx/example-app/blob/master/src/effects/book.ts Ma question est pourquoi, dans la première @Effet, il utilise switchMap tandis que l'autre utilise mergeMap. Est-ce parce que le premier @Effet est de traiter avec le réseau, et avec le switchMap vous pouvez annuler la précédente réseau demande si il fonctionne?

@Effect() search$ = this.updates$
.whenAction(BookActions.SEARCH)
.map<string>(toPayload)
.filter(query => query !== '')
.switchMap(query => this.googleBooks.searchBooks(query)
.map(books => this.bookActions.searchComplete(books))
.catch(() => Observable.of(this.bookActions.searchComplete([])))
);
@Effect() clearSearch$ = this.updates$
.whenAction(BookActions.SEARCH)
.map<string>(toPayload)
.filter(query => query === '')
.mapTo(this.bookActions.searchComplete([]));
@Effect() addBookToCollection$ = this.updates$
.whenAction(BookActions.ADD_TO_COLLECTION)
.map<Book>(toPayload)
.mergeMap(book => this.db.insert('books', [ book ])
.mapTo(this.bookActions.addToCollectionSuccess(book))
.catch(() => Observable.of(
this.bookActions.addToCollectionFail(book)
))
);
@Effect() removeBookFromCollection$ = this.updates$
.whenAction(BookActions.REMOVE_FROM_COLLECTION)
.map<Book>(toPayload)
.mergeMap(book => this.db.executeWrite('books', 'delete', [ book.id ])
.mapTo(this.bookActions.removeFromCollectionSuccess(book))
.catch(() => Observable.of(
this.bookActions.removeFromCollectionFail(book)
))
);
}
  • Je suis venu ici à la recherche de ce qu'est la différence, l'explication la plus claire que j'ai rencontré est: When you hear the word merge, think — use every­thing on all the streams aka. merge every­thing. Whereas when you hear the word switch, think — switch to using data on the newer stream javascript.tutorialhorizon.com/2017/03/29/...
InformationsquelleAutor Tuong Le | 2016-07-21