[RxJS] Groupby operator
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的更多相关文章
- rxjs自定义operator
rxjs自定义operator
- [RxJS] Creation operator: of()
RxJS is a lot about the so-called "operators". We will learn most of the important operato ...
- [RxJS] Connection operator: multicast and connect
We have seen how Subjects are useful for sharing an execution of an RxJS observable to multiple obse ...
- [RxJS] Transformation operator: repeat
Operator repeat() is somewhat similar to retry(), but is not for handling operators. In this lesson ...
- [RxJS] Transformation operator: buffer, bufferCount, bufferTime
This lesson will teach you about another horizontal combination operator: buffer and its variants. B ...
- [RxJS] Transformation operator: scan
All of the combination operators take two or more observables as input. These operators may also be ...
- [RxJS] Combination operator: withLatestFrom
Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLate ...
- [RxJS] Combination operator: combineLatest
While merge is an OR-style combination operator, combineLatest is an AND-style combination operator. ...
- [RxJS] Filtering operator: filter
This lesson introduces filter: an operator that allows us to let only certain events pass, while ign ...
随机推荐
- AVR单片机教程——数字输出
从上一篇教程中我们了解到,按键与开关的输入本质上就是数字信号的读取.这一篇教程要讲的是,控制LED的原理是数字信号的输出.数字IO是单片机编程之有别于桌面编程的各项内容中最简单.最基础的. 在讲数字信 ...
- javascript匿名函数自执行 (function(window,document,undefined){})(window,document);
使用匿名自执行函数的作用: (function(window,document,undefined){})(window,document); 1.首先匿名函数 (function(){}) (); ...
- Selenium 配置IE浏览器
1.安装selenium pip install selenium 2.安装IE浏览器driver http://selenium-release.storage.googleapis.com/ind ...
- 关于fastjson与jackson在反序列化bool型时的区别
背景 在测试中,两个项目a,b的接口参数用到了Bool类型,当传参"0",项目a通过了,项目b报错了,排查了下,项目b的那个接口,在对传参反序列化时就出现了问题,最后发现两个项目使 ...
- appium1.6在mac上环境搭建启动ios模拟器上Safari浏览器 转自:上海-悠悠
前言 在mac上搭建appium踩了不少坑,先是版本低了,启动后无限重启模拟器.后来全部升级最新版本,就稳稳的了. 环境准备: 1.OS版本号10.12 2.xcode版本号8.3.2 3.appiu ...
- Python之TensorFlow的变量收集、自定义命令参数、矩阵运算、梯度下降-4
一.TensorFlow为什么要存在变量收集的过程,主要目的就是把训练过程中的数据,比如loss.权重.偏置等数据通过图形展示的方式呈现在开发者的眼前. 自定义参数:自定义参数,主要是通过Python ...
- Java调用Http/Https接口(1)--编写服务端
Http接口输入的数据一般是键值对或json数据,返回的一般是json数据.本系列文章主要介绍Java调用Http接口的各种方法,本文主要介绍服务端的编写,方便后续文章里的客户端的调用.文中所使用到的 ...
- springboot中常用的依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- Swift之xib模块化设计
一.解决问题 Xib/Storybarod可以方便.可视化的设置约束,在开发中也越来越重要.由于Xib不能组件化,使得封装.重用都变得不可行.本文将介绍一种解决方案,来实现Xib组件化. 二.模型块原 ...
- Fortify漏洞之Denial of Service: Regular Expression
继续对Fortify的漏洞进行总结,本篇主要针对 Denial of Service: Regular Expression 漏洞进行总结,如下: 1.Denial of Service: Reg ...