RXJS: assigning and then reusing value between operators

问题: Suppose I have a function GetUserRecommendedSongs. It does the following: It presents the user with a dialog: what is your mood today? (happygloomynostalgic) Using...

问题:

Suppose I have a function GetUserRecommendedSongs. It does the following:

  1. It presents the user with a dialog: what is your mood today? (happygloomynostalgic)
  2. Using the result it calls a service GetUserRecommendationsByMood.
  3. It then returns a result for example: {mood: "nostalgic", songIds: [12, 25]};
  4. The caller of this function (possibly several ui components) would use the result to play the songs under the title "so you feel ${result.mood} today?"

The problem is I use the mood twice: to get the recommendations, and in the final result.

With asyncawait I would do:

const requiredMood = await ShowRequiredMoodDialog();
//handle cancellation e.g. if(!mood)
let recommendedSongs = await GetUserRecommendations(mood);
return {mood, recommendedSongs};

However with rxjs I was only able to come up with the following:

let mood$ = ShowRequiredMoodDialog().pipe(share) //has to be shared so we don't show the dialog twice
let recommendedSongs$ = mood$.pipe(switchMap((mood)=> GetUserRecommendations(mood)));
return forkJoin(mood$, recommendedSongs$) //with some selectormap to turn into object

(note to reader: don't use this as a reference to rxjs, as I did not test this code)

This code is quite complicated. Can it be simplified?


回答1:

The only different way that comes to my mind is like this but I don't know which one is more readable:

mood$.pipe(
  switchMap(mood => GetUserRecommendations(mood).pipe(
    map(recommendedSongs => [mood, recommendedSongs]),
  ),
)

回答2:

What about this one?

let mood$ = ShowRequiredMoodDialog().pipe(share());
let recommend = (mood) => {
  return { mood, songs: GetUserRecommendations(mood) };
};
let recommendedSongs$ = mood$.pipe(switchMap(recommend));
return recommendedSongs$;
  • 发表于 2019-01-15 00:23
  • 阅读 ( 225 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除