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

    In currently implemention, there is one problem, when the page load and click refresh button, the user list is not immediatly clean up,it is cleared after the new data come in.

    So two things we need to do, 

     1. Hide User Elements when the user object is null

     2. clean the list when page first load

     3. clean the list after each refresh click

    1. In the renderSuggestion method, check the suggestedUser is null or not, if it is null, then we hide the user item.

    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;
      }
    }

    2. So when the page load, you can see the frash, because we have placeholder in the html:

        <ul class="suggestions">
            <li class="suggestion1">
                <img />
                <a href="#" target="_blank" class="username">this will not be displayed</a>
                <a href="#" class="close close1">x</a>
            </li>
            <li class="suggestion2">
                <img />
                <a href="#" target="_blank" class="username">neither this</a>
                <a href="#" class="close close2">x</a>
            </li>
            <li class="suggestion3">
                <img />
                <a href="#" target="_blank" class="username">nor this</a>
                <a href="#" class="close close3">x</a>
            </li>
        </ul>

    When the data comes in, it will be replaced with data, it will be obvious when the low speed internet. 

    SO in the createSuggestion method, we use startWith(null), it set suggestionUser to null:

    function createSuggestionStream(responseStream) {
      return responseStream.map(listUser =>
        listUser[Math.floor(Math.random()*listUser.length)]
      )
      .startWith(null);
    }

    This will solve the problem when first loading, the user list frashing...

    3. In each responseStream, we merge with refreshClickStream and return null value. Because network request take time to finish, so the null value will effect the responseStream first, so the user item can be hided.

    function createSuggestionStream(responseStream) {
      return responseStream.map(listUser =>
        listUser[Math.floor(Math.random()*listUser.length)]
      )
      .startWith(null)
      .merge(refreshClickStream.map(ev => null));
    }
    //----r---------------------r---------->
    //         startWith(N)
    //N---r------------------------------->
    //------------------N-----N----------->
    //   merge
    //N---r-----------------r---r---------->
    //

    --------------------------

    var refreshButton = document.querySelector('.refresh');
    
    var refreshClickStream = Rx.Observable.fromEvent(refreshButton, '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 responseStream = startupRequestStream.merge(requestOnRefreshStream)
      .flatMap(requestUrl =>
        Rx.Observable.fromPromise(jQuery.getJSON(requestUrl))
      );
    
    function createSuggestionStream(responseStream) {
      return responseStream.map(listUser =>
        listUser[Math.floor(Math.random()*listUser.length)]
      )
      .startWith(null)
      .merge(refreshClickStream.map(ev => null));
    }
    
    var suggestion1Stream = createSuggestionStream(responseStream);
    var suggestion2Stream = createSuggestionStream(responseStream);
    var suggestion3Stream = createSuggestionStream(responseStream);
    
    // 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');
    });
    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://code.jquery.com/jquery-1.7.2.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.8/rx.all.js"></script>
      <meta charset="utf-8">
      <title>JS Bin</title>
    </head>
    <body>
    <div class="container">
        <div class="header">
             <h3>Who to follow</h3><a href="#" class="refresh">Refresh</a>
        </div>
        <ul class="suggestions">
            <li class="suggestion1">
                <img />
                <a href="#" target="_blank" class="username">this will not be displayed</a>
                <a href="#" class="close close1">x</a>
            </li>
            <li class="suggestion2">
                <img />
                <a href="#" target="_blank" class="username">neither this</a>
                <a href="#" class="close close2">x</a>
            </li>
            <li class="suggestion3">
                <img />
                <a href="#" target="_blank" class="username">nor this</a>
                <a href="#" class="close close3">x</a>
            </li>
        </ul>
    </div>
    </body>
    </html>
  • 相关阅读:
    jvm基本结构和解析
    多态的意思
    java中对象的简单解读
    double类型和int类型的区别
    python 解析xml文件
    win10不能映射Ubuntu共享文件
    Qt程序打包
    Ubuntu boot分区文件误删,系统无法启动,怎么解
    ubuntu Boot空间不够问题“The volume boot has only 5.1MB disk space remaining”
    Ubuntu 分辨率更改 xrandr Failed to get size of gamma for output default
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5255822.html
Copyright © 2011-2022 走看看