首页 > Web开发 > 详细

[RxJS] Groupby operator

时间:2019-10-24 20:18:15      阅读:96      评论:0      收藏:0      [点我收藏+]

The use case is similar to Twitter "like" button, you can click "click" button on different post, each "like" button are isolated, it preforms optimistic UI render, handling the back-press on backend, cancel previous request only for the same twitter id.

 

In the talk of RxJS. It use Movie as example.

 

So, if you have similar use case, this piece of code can help:

 

import { Observable, fromEvent, Subject, EMPTY } from rxjs;
import { tap, mergeMap, groupBy, timeoutWith, ignoreElements, switchMap } from rxjs/operators;

const actions$ = dispatcher.asObservable().pipe(
  // optimize ui rendering
  tap(({ movieId }) => setButtonEmoji(movieId)),
  // group all the request by movieId
  groupBy(
    movie => movie.movieId,
    movie => movie,
    // cancel the extra requests to prevent memory leak by set 15s idel time
    actionsByGroup$ =>
      actionsByGroup$.pipe( 
        timeoutWith(15000, EMPTY),
        ignoreElements()
      )
  ),
  // for each group of request, we apply switchMap to cancel previous request
  // finally flatten the requests into one
  mergeMap(group$ => group$.pipe(switchMap(movie => toggleStatus(movie.movieId))))
);

 

[RxJS] Groupby operator

原文:https://www.cnblogs.com/Answer1215/p/11733946.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!