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.

  • 相关阅读:
    前端工程师必备的7个chrome插件
    树莓派 基于Web的温度计
    vue2.0 非父子组件如何通信
    newman
    mysql主从同步设置
    redis集群搭建
    服务器之间共享挂载
    Jenkins自动构建-部署-测试
    postman使用整理
    Charles使用
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5256574.html
Copyright © 2011-2022 走看看