要求对此进行跟进(How to return observable from subscribe), 因为公认的解决方案没有解决我的用例 这是我的代码
@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())
}
控制台上的输出是:
开始搜索:Object {player:Object,nowPlaylist:Object, nowChannellist:Object,user:Object,search:Object ...}
输入了searchAll
PS:如果我直接调用search()(它将有一个硬编码的通道),那么上面的工作就是searchAll()
如果我可以直接将多个调用链接到search()然后将它们合并为@Effect()searchQuery$
本身的一部分,我也没问题。
答案 0 :(得分:0)
我会尝试发布答案,但我没有完整的要求所以这是一种猜测。
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))