【问题标题】:return subject from inside map从内部地图返回主题
【发布时间】:2017-09-08 23:10:10
【问题描述】:

作为对此的跟进提出此问题 (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:对象,用户:对象,搜索:对象……}

进入搜索全部

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

【问题讨论】:

  • @günter-zöchbauer:我错过了什么,我正在尝试做你在这里提到的事情(stackoverflow.com/a/39935735/865220
  • @andré-werlang:嗨,andré,我一直在关注你的 rxjs 答案,也许你能帮忙。
  • @artant:你能帮忙吗? :)
  • searchAll 看起来不正确。 1)您在地图中重复使用相同的主题并完成它 2)没有人订阅 nowChannellist$ 并且它不会运行,因为 observables 很懒
  • 我看不懂你在(1)中的意思,你能给代码吗?

标签: angular rxjs observable ngrx ngrx-effects


【解决方案1】:

我会尝试发布答案,但我没有完整的要求,所以这是一种猜测。

   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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    相关资源
    最近更新 更多