zoukankan      html  css  js  c++  java
  • [RxJS] Reactive Programming

    Currently we show three users in the list, it actually do three time network request, we can verfiy this by console out each network request:

    var responseStream = startupRequestStream.merge(requestOnRefreshStream)
      .flatMap(requestUrl => {
        console.log('do network request');
        return Rx.Observable.fromPromise(jQuery.getJSON(requestUrl));
      });

    We actually can use the same network request by shareReplay(1):

    var responseStream = startupRequestStream.merge(requestOnRefreshStream)
      .flatMap(requestUrl => {
        console.log('do network request');
        return Rx.Observable.fromPromise(jQuery.getJSON(requestUrl));
      })
      .shareReplay(1);

    Why replay one? Well, that's because if there happens to be a really late subscriber...let's say someone does a setTimeout and doesn't subscribe to the responseStream after a long while. Let's say, three seconds or even 10 seconds. Then, this subscribe will get a replayed response JSON. It will not do a new network request, but it will simply replay that same JSON that happened before.

  • 相关阅读:
    四则运算————javaweb版
    构建之法阅读笔记02
    十一周学习进度条
    软工概论-课堂练习:添加信息
    梦断代码阅读笔记01
    链接doc命令行的mysql的编码问题
    构建之法阅读笔记01
    第十周学习进度条
    web
    sql初——基础
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5256574.html
Copyright © 2011-2022 走看看