问题
Asking this as a followup on this (How to return observable from subscribe), as the accepted solution didn't solve my use case Here is my code
@Effect()
searchQuery$ = this.actions$
.ofType(PlayerSearchActions.SEARCH_NEW_QUERY)
.map(toPayload)
.withLatestFrom(this.store)
.map((latest: any[]) => latest[1])
.do((res)=>{console.log("Starting search with:",res);})
.switchMap((store: MyState) =>
this.youtubeSearch.resetPageToken()
.searchAll(store.search.query, store.search.queryParams)
.do((res)=>{console.log("Effects all:",res);})
.map((youtubeResponse) => this.playerSearchActions.searchResultsReturned(youtubeResponse))
.do((res)=>{console.log("Effects intermediate",res);})
).do((res)=>{console.log("Effects complete",res);});
searchAll(query: string, params?: any) {
console.log('entered searchAll');
let subject = new Subject();
this.nowChannellist$.map(channels => channels.map(channel => {
this._apiOptions.channelId = channel.channelId;
console.log('entered searchAll for channel:',channel.name);
subject.next(this.search(query, params));
subject.complete();
return channel;
})).do((res) => { console.log('searchAll done', res); });
return subject;
}
search(query: string, params?: any) {
//....some code operating on query and params
return this.http.get('https://www.googleapis.com/youtube/v3/search', _options)
.map(response => response.json())
}
The output on console is :
Starting search with: Object {player: Object, nowPlaylist: Object, nowChannellist: Object, user: Object, search: Object…}
entered searchAll
PS: The above works if I call search() directly(which will have one hardcoded channels) instad of searchAll()
I am also fine if I can chain multiple calls to search() directly and then merge them as part of @Effect()searchQuery$
itself.
回答1:
I will try to post the answer but I do not have the full requirements so it is kind of guess.
let nowChannellist$ = new Rx.Observable.of([{channelId:1}, {channelId:2}]);
nowChannellist$.switchMap(channels => {
let channels$ = channels.map(channel => {
return this.search(chanel);
});
return Rx.Observable.forkJoin(channels$);
})
.subscribe(x=>console.log(x))
来源:https://stackoverflow.com/questions/43381922/return-subject-from-inside-map