-1

将此作为对此的跟进(如何从订阅返回可观察的),因为接受的解决方案没有解决我的用例这是我的代码

  @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())
  }

控制台上的输出是:

开始搜索:对象 {player: Object, nowPlaylist: Object, nowChannellist: Object, user: Object, search: Object…}

输入搜索全部

PS:如果我直接调用 search()(它将有一个硬编码通道)而不是 searchAll(),则上述方法有效,如果我可以直接链接多个对 search() 的调用,然后将它们合并为@Effect()searchQuery$自身的一部分,我也很好。

4

1 回答 1

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))
于 2017-04-25T00:53:23.927 回答