[RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()
So now we want to replace one user when we click the 'x' button.
To do that, we want:
1. Get the cached network data for generating the userList.
2. Then get a random user from the cached data.
3. Showing the user in the list.
We have the function to create suggestion user:
function createSuggestionStream(responseStream, closeClickStream) {
return responseStream.map(getRandomUser)
.startWith(null)
.merge(refreshClickStream.map(ev => null))
}
It contains the 'responseStream' which contains the cached data we need.
So, the code would somehow like this:
var close1Clicks = Rx.Observable.fromEvent(closeButton1, 'click');
close1Clicks.withLatestFrom(responseStream, (clickEv, cachedData) => {
return getRandomUser(cachedData);
});
When the closeClickStream happens, it will fetch the cached data and send userList to getRandomUser() function to pick user.
Now, we can merge this stream with responseStream:
function createSuggestionStream(responseStream, closeClickStream) {
return responseStream.map(getRandomUser)
.startWith(null)
.merge(refreshClickStream.map(ev => null))
.merge(
closeClickStream.withLatestFrom(responseStream,
(clickEv, cachedData) => getRandomUser(cachedData))
);
}
-----------
var refreshButton = document.querySelector('.refresh');
var closeButton1 = document.querySelector('.close1');
var closeButton2 = document.querySelector('.close2');
var closeButton3 = document.querySelector('.close3'); var refreshClickStream = Rx.Observable.fromEvent(refreshButton, 'click');
var close1Clicks = Rx.Observable.fromEvent(closeButton1, 'click');
var close2Clicks = Rx.Observable.fromEvent(closeButton2, 'click');
var close3Clicks = Rx.Observable.fromEvent(closeButton3, 'click'); var startupRequestStream = Rx.Observable.just('https://api.github.com/users'); var requestOnRefreshStream = refreshClickStream
.map(ev => {
var randomOffset = Math.floor(Math.random()*500);
return 'https://api.github.com/users?since=' + randomOffset;
}); var requestStream = startupRequestStream.merge(requestOnRefreshStream); var responseStream = requestStream
.flatMap(requestUrl =>
Rx.Observable.fromPromise(jQuery.getJSON(requestUrl))
)
.shareReplay(1); // refreshClickStream: -------f------------->
// requestStream: r------r------------->
// responseStream: ---R-------R--------->
// closeClickStream: ---------------x----->
// suggestion1Stream: N--u---N---u---u-----> function getRandomUser(listUsers) {
return listUsers[Math.floor(Math.random()*listUsers.length)];
} function createSuggestionStream(responseStream, closeClickStream) {
return responseStream.map(getRandomUser)
.startWith(null)
.merge(refreshClickStream.map(ev => null))
.merge(
closeClickStream.withLatestFrom(responseStream,
(clickEv, cachedData) => getRandomUser(cachedData))
);
} var suggestion1Stream = createSuggestionStream(responseStream, close1Clicks);
var suggestion2Stream = createSuggestionStream(responseStream, close2Clicks);
var suggestion3Stream = createSuggestionStream(responseStream, close3Clicks); // Rendering ---------------------------------------------------
function renderSuggestion(suggestedUser, selector) {
var suggestionEl = document.querySelector(selector);
if (suggestedUser === null) {
suggestionEl.style.visibility = 'hidden';
} else {
suggestionEl.style.visibility = 'visible';
var usernameEl = suggestionEl.querySelector('.username');
usernameEl.href = suggestedUser.html_url;
usernameEl.textContent = suggestedUser.login;
var imgEl = suggestionEl.querySelector('img');
imgEl.src = "";
imgEl.src = suggestedUser.avatar_url;
}
} suggestion1Stream.subscribe(user => {
renderSuggestion(user, '.suggestion1');
}); suggestion2Stream.subscribe(user => {
renderSuggestion(user, '.suggestion2');
}); suggestion3Stream.subscribe(user => {
renderSuggestion(user, '.suggestion3');
});
[RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()的更多相关文章
- [RxJS] Reactive Programming - Rendering on the DOM with RxJS
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...
- [RxJS] Reactive Programming - What is RxJS?
First thing need to understand is, Reactive programming is dealing with the event stream. Event stre ...
- [RxJS] Reactive Programming - Clear data while loading with RxJS startWith()
In currently implemention, there is one problem, when the page load and click refresh button, the us ...
- [RxJS] Reactive Programming - Sharing network requests with shareReplay()
Currently we show three users in the list, it actually do three time network request, we can verfiy ...
- [Reactive Programming] Async requests and responses in RxJS
We will learn how to perform network requests to a backend using RxJS Observables. A example of basi ...
- [RxJS] Reactive Programming - New requests from refresh clicks -- merge()
Now we want each time we click refresh button, we will get new group of users. So we need to get the ...
- [RxJS] Reactive Programming - Why choose RxJS?
RxJS is super when dealing with the dynamic value. Let's see an example which not using RxJS: var a ...
- [Reactive Programming] RxJS dynamic behavior
This lesson helps you think in Reactive programming by explaining why it is a beneficial paradigm fo ...
- .Net中的反应式编程(Reactive Programming)
系列主题:基于消息的软件架构模型演变 一.反应式编程(Reactive Programming) 1.什么是反应式编程:反应式编程(Reactive programming)简称Rx,他是一个使用LI ...
随机推荐
- Log4Qt使用(三)在DailyRollingFileAppender类中增加属性mMaxBackupIndex
在Log4Qt中存在一个比较大的问题,当使用 DailyRollingFileAppender对日志进行输出时,会无限输出文件,也就是说,当系统运行很久时,日志文件有可能很大,大到无法想象.因此,很多 ...
- c#中从string数组转换到int数组
以前一直有一个数组之间转换的东西,可是忘记了,今天也是找了好久也没有解决,最后用这种方法解决了,分享给大家. " }; int[] output = Array.ConvertAll< ...
- Append加载动态轮播
前几天遇到了些小麻烦,不过很快就解决了.之所以要记下来是因为作为一名前端的程序员,要理解页面的加载顺序是最重要的.要不然自己写程序意外的出现bug~~ 刚开始写利用Append的时候,利用火狐的fir ...
- 011_hasCycle
/* * Author :SJQ * * Time :2014-07-16-20.21 * */ #include <iostream> #include <cstdio> # ...
- 编译osg for android
做osg数数已经快两年了,之前将一些opengl的代码搬到了osg上,现在将一些osg的代码搬到了android上,尝试看看效果. 首先是编译的事情,android for android在http: ...
- sql 查找数据库中某字符串所在的表及字段
declare @str varchar(100) set @str='是否严格控制' --要搜索的字符串 declare @s varchar(8 ...
- html Table实现表头固定
最近一直在搞前台琐碎的东西,也学习了一下linux,没有时间对新的东西进行深入的研究和学习,没有写博客,不过归咎其原因还是在于自己的惰怠. 废话不多说,今天想将一个前台页面设计的一个小东西分享一下,那 ...
- struts2标签自动错行、换行问题
<s:radio list="#{0:'男',1:'女' }" value="student.sex" name="student.sex&qu ...
- Linux下*.tar.gz文件解压缩命令 find 命令
1.压缩命令: 命令格式:tar -zcvf 压缩文件名.tar.gz 被压缩文件名 可先切换到当前目录下.压缩文件名和被压缩文件名都可加入路径. 2.解压缩命令: 命令格式:tar -z ...
- 在Mac上使用Nginx和FastCGI部署Flask应用
最近在学习Flask,本文介绍一下如何部署Flask开发的应用,同时也学习一下Nginx的使用,这只是在Mac上的一个实验. 应用 这里使用的应用就是官方的文档中给出的Flaskr. 安装Nginx ...