[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 ...
随机推荐
- Ubuntu 14.04 下手动安装Firefox的Flash插件
有时候我们不得不採用手动安装一些软件. Ubuntu 14.04 下手动安装Firefox的Flash插件有下面几步 1. 下载Flash插件 下载地址为http://get.adobe.com/cn ...
- C#使用 SQLite 数据库 开发的配置过程及基本操作类,实例程序:工商银行贵金属行情查看小工具
--首发于博客园, 转载请保留此链接 博客原文地址 本文运行环境: Win7 X64, VS2010 1. SQLite 的优点: SQLite 是一款轻型数据库,开发包只有十几M, 相对于 MSS ...
- 国外.net学习资源网站
转载 :出处:http://www.cnblogs.com/kingjiong/ 名称:快速入门地址 http://chs.gotdotnet.com/quickstart/ 描述:本站点是微软.NE ...
- CSharp命名风格
1.大小写约定 为了区分一个标识符中的多个单词,把标识符中的每个单词的首字母大写.不要用下划线来区分单词,或者在标识符中任何地方使用下划线,有两种方式适合大写标识符的字母: PascalCasing( ...
- Sass运算
加法在 CSS 中能做运算的,到目前为止仅有 calc() 函数可行.在 Sass 中,运算只是其基本特性之一.在 Sass 中可以做各种数学计算.加法运算是 Sass 中运算中的一种,在变量或属性中 ...
- ASP.NET常用编程代码(二)
1.绑定在DataList中的DropDownList private void dlistOrder_EditCommand(object source, System.Web.UI.WebCont ...
- ZOJ3556 How Many Sets I(容斥)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud How Many Sets I Time Limit: 2 Seconds ...
- Visual Studio发布项目到远程服务器的步骤
第一步: 需要远程服务器上安装Web Deploy ,下载地址:http://www.iis.net/downloads/microsoft/web-deploy PS.安装时选择完全安装. 第二步: ...
- js 获取mac地址
js 获取mac地址 function MacInfo(){ var locator =new ActiveXObject ("WbemScripting.SWbemLocator" ...
- Python 番外 消息队列设计精要
消息队列已经逐渐成为企业IT系统内部通信的核心手段.它具有低耦合.可靠投递.广播.流量控制.最终一致性等一系列功能,成为异步RPC的主要手段之一.当今市面上有很多主流的消息中间件,如老牌的Active ...