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. Linux 进程间通信(管道、共享内存、消息队列、信号量)

           进程通信 : 不同进程之间传播或交换信息    为什么要进程通信呢? 协同运行,项目模块化 通信原理 : 给多个进程提供一个都能访问到的缓冲区. 根据使用场景,我们能划分为以下几种通信 ...

  2. R学习笔记3 数据处理

    1,日期类型 日期类型比较特殊,日期值通常以字符串的形式输入到R中,然后使用as.Date()函数转换为以数值形式存储的日期变量 mydate <- as.Date("2019-01- ...

  3. quartz2.3.0(九)job任务监听器,监听任务执行前、后、取消手动处理方法

    job1任务类 package org.quartz.examples.example9; import java.util.Date; import org.quartz.Job; import o ...

  4. Python模拟知乎登录

    # -*- coding:utf-8 -*- import urllib import urllib2 import cookielib import time from PIL import Ima ...

  5. Java日志logback使用

    pom中添加: <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</ ...

  6. Redis之RDB和AOF持久化介绍

    什么是数据库状态 redis是一个键值对的数据库服务器,服务器中通常包含中任意个非空的数据库,而每个数据库又可以包含任意个键值对,为了方便起见,我们将服务器中的非空数据库以及他们的键值对统称为数据库状 ...

  7. Oracle——无法在查询中执行 DML 操作

    今天在调用Oracle Function遇到一个异常

  8. 可拖拽dialog

    指令的封装转自https://blog.csdn.net/sinat_21902709/article/details/86545444 可拖拽dialog应用于很多弹出框,所以需要作用于全局 在插件 ...

  9. JS中的迭代器和生成器

    利用迭代器生成一个遍历方法: let arr1 = [1, 2, 3, 11, 22, 13, 24]; function forOf(arr, callback) { // 找到迭代器函数 let ...

  10. Java System Reports

    You use Java System Reports as a problem detection and analysis tool to: ●      Monitor the AS Java ...