zoukankan      html  css  js  c++  java
  • [RxJS] Refactoring Composable Streams in RxJS, switchMap()

    Refactoring streams in RxJS is mostly moving pieces of smaller streams around. This lessons demonstrates a simple refactoring by requiring the StopWatch to be more configurable.

     Code we have now:
    const Observable = Rx.Observable;
    
    const startButton = document.querySelector('#start');
    const stopButton = document.querySelector('#stop');
    const resetButton = document.querySelector('#reset');
    
    const start$ = Observable.fromEvent(startButton, 'click');
    const interval$ = Observable.interval(1000);
    const stop$ = Observable.fromEvent(stopButton, 'click');
    const reset$ = Observable.fromEvent(resetButton, 'click');
    
    
    const data = {count:0};
    const inc = (acc)=> ({count: acc.count + 1});
    const reset = (acc)=> data;
    
    const intervalThatStops$ = interval$
        .takeUntil(stop$);
    
    const incOrReset$ = Observable.merge(
        intervalThatStops$.mapTo(inc),
        reset$.mapTo(reset)
    );
    
    start$
        .switchMapTo(incOrReset$)
        .startWith(data)
        .scan((acc, curr)=> curr(acc))
        .subscribe((x)=> console.log(x));

    Currently the interval is hard code 1000, we want it more flexable:

    const Observable = Rx.Observable;
    
    const startButton = document.querySelector('#start');
    const halfButton = document.querySelector('#half');
    const quarterButton = document.querySelector('#quarter');
    
    const stopButton = document.querySelector('#stop');
    const resetButton = document.querySelector('#reset');
    
    const start$ = Observable.fromEvent(startButton, 'click');
    const half$ = Observable.fromEvent(halfButton, 'click');
    const quarter$ = Observable.fromEvent(quarterButton, 'click');
    
    const stop$ = Observable.fromEvent(stopButton, 'click');
    const reset$ = Observable.fromEvent(resetButton, 'click');

    We added two more buttons, halfButton set interval as 500, and quarterButton is 250.

    So when we click "start", "half" or "quarter" button, the interval will set differently.

     Observable.merge(
        start$.mapTo(1000),
        half$.mapTo(500),
        quarter$.mapTo(250)
     )

    We want pass the time (1000, 500, 250) to the next observable to control the time.

     Observable.merge(
        start$.mapTo(1000),
        half$.mapTo(500),
        quarter$.mapTo(250)
     )
      .switchMap(
        (time) => {
          return Observable.merge(
            Observable.interval(time)
            .takeUntil(stop$)
            .mapTo(inc),
            reset$.mapTo(reset)
          )
        }
      )

    We use switchMap() instead of switchMapTo() is becuase switchMapTo() accept observable as param and switchMap accpet a function which return an Observable. So use switchMap() is more flexable.

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

    const Observable = Rx.Observable;
    
    const startButton = document.querySelector('#start');
    const halfButton = document.querySelector('#half');
    const quarterButton = document.querySelector('#quarter');
    
    const stopButton = document.querySelector('#stop');
    const resetButton = document.querySelector('#reset');
    
    const start$ = Observable.fromEvent(startButton, 'click');
    const half$ = Observable.fromEvent(halfButton, 'click');
    const quarter$ = Observable.fromEvent(quarterButton, 'click');
    
    const stop$ = Observable.fromEvent(stopButton, 'click');
    const reset$ = Observable.fromEvent(resetButton, 'click');
    
    
    const data = {count:0};
    const inc = (acc)=> ({count: acc.count + 1});
    const reset = (acc)=> data;
    
     Observable.merge(
        start$.mapTo(1000),
        half$.mapTo(500),
        quarter$.mapTo(250)
     )
      .switchMap(
        (time) => {
          return Observable.merge(
            Observable.interval(time)
            .takeUntil(stop$)
            .mapTo(inc),
            reset$.mapTo(reset)
          )
        }
      )
     .startWith(data)
     .scan( (acc, curr) => curr(acc))
     .subscribe( (z) => console.log(z) );
  • 相关阅读:
    ajax的post提交方式和传统的post提交方式哪个更快?
    请问具体到PHP的代码层面,改善高并发的措施有哪些
    TP为什么这个if判断什么都不显示?
    如何用正则匹配这段文本
    七牛上图片总是net::ERR_NAME_NOT_RESOLVED
    该如何来开发这个喜欢的功能呢?
    打包phar文件过大的问题。
    .map(function(item)...)这个是按hashcode自动遍历的,怎么才能按照我想要的顺序遍历呢?
    Java操作Kafka执行不成功
    webkit事件处理
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5264164.html
Copyright © 2011-2022 走看看