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(, 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的更多相关文章

  1. rxjs自定义operator

    rxjs自定义operator

  2. [RxJS] Creation operator: of()

    RxJS is a lot about the so-called "operators". We will learn most of the important operato ...

  3. [RxJS] Connection operator: multicast and connect

    We have seen how Subjects are useful for sharing an execution of an RxJS observable to multiple obse ...

  4. [RxJS] Transformation operator: repeat

    Operator repeat() is somewhat similar to retry(), but is not for handling operators. In this lesson ...

  5. [RxJS] Transformation operator: buffer, bufferCount, bufferTime

    This lesson will teach you about another horizontal combination operator: buffer and its variants. B ...

  6. [RxJS] Transformation operator: scan

    All of the combination operators take two or more observables as input. These operators may also be ...

  7. [RxJS] Combination operator: withLatestFrom

    Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLate ...

  8. [RxJS] Combination operator: combineLatest

    While merge is an OR-style combination operator, combineLatest is an AND-style combination operator. ...

  9. [RxJS] Filtering operator: filter

    This lesson introduces filter: an operator that allows us to let only certain events pass, while ign ...

随机推荐

  1. session知识点小结

    Session: 1. 概念:服务器端会话技术,在一次会话的多次请求间共享数据,将数据保存在服务器端的对象HttpSession中. 2. 快速入门: 1. 获取HttpSession对象: Http ...

  2. Python之序列化概念

    我们把对象(变量)从内存中变成可存储或运输的过程称之为序列化,在 Python 中叫 pickling ,在其他的语言中也被称之为 serialization,marshalling,flatteni ...

  3. 在window10安装Mysql

    一:下载 1.1下载地址:https://dev.mysql.com/downloads/mysql/ 1.2解压 D:\InstallSoftWare\MYSQL 2环境变量配置    path里添 ...

  4. pymysql 模块简单使用

    目录 pymysql 模块简单使用 安装 pymysql 模块 使用 pymysql 连接数据库 并插入数据 使用pymysql 插入数据 修改查询显示结果 pymysql 模块简单使用 安装 pym ...

  5. nRF24L01P数据传输速率

    项目要用nRF24L01P做语音的传输,数据量可想而知. 发送端按照8KHz/s采样率,每次采样双声道,16位深度(2Bytes). 数据量算一下就是8000x2x2=32000(Bytes) nRF ...

  6. proxy_banner

  7. Linux 系统中如何进入退出 vim 编辑器

    在 Linux 中,vim 编辑器是系统自带的文本编辑器,但要修改某个文本文件,可不是像 Windows 那样操作,更有新手,进入 vi 编辑器后,无法退出以致于强制关机,其实,这个vim(vi)也是 ...

  8. text-overflow 全兼容

    text-overflow 全兼容 text-overflow 这个CSS属性用于设置或检索是否使用一个省略标记(...)标示对象内文本的溢出.比起在后台用程序截断文本再附上省略标记的做法,用CSS来 ...

  9. jQuery常用知识点大总结

    目录 jQuery jQuery介绍 jQuery的优势 jQuery的引入方式有两种: jQuery对象和dom对象 jQuery选择器 基本选择器(同css) 基本筛选器(选择之后进行过滤): 属 ...

  10. AndroidStudio中Run按钮是灰色的解决方法

    在model下拉框中选择app.如果下拉框中没有app,(没有工程名),那么请先去设置: Android Studio 3.3.0 File->sync project with gradles ...