zoukankan      html  css  js  c++  java
  • [RxJS] Transformation operator: bufferToggle, bufferWhen

    bufferToggle(open: Observable, () => close: Observalbe : Observalbe<T[]>)

    bufferToggle take two args, first is opening observable, seconde is a function which return an observable for closing.

    The closeing observalbe only execute after opening emit value.

    const source$ = Rx.Observable.interval(500);
    const open$ = Rx.Observable.interval(1500);
    const close$ = Rx.Observable.interval(1000);
    
    /**
    
    ---0---1---2---3---4---5---6---7---8---9----....    (source)
    
    -----------1-----------2-----------3--------...      (open)
    
               --- ---x    --- ---x    --- ---x...      (close)
            bufferToggle(open$, () => close$)
            
    ------------------([2,3])-----([5.6])-----([8,9])--...
    */
    
    
    const foo$ = source$.bufferToggle(open$, () => {
      return close$;
    });
    
    foo$.subscribe(
      (x) => console.debug("Next: " + x),
      (err) => console.error(err),
      () => console.info("DONE")
    )
    
    /*
    
    "Next: 2,3"
    "Next: 5,6"
    "Next: 8,9"
    "Next: 11,12"
    ...
    */

    bufferWhen( () => Observable):

    bufferWhen takes a function which return observable.

    const source$ = Rx.Observable.interval(500);
    const close$ = Rx.Observable.interval(1000);
    
    /**
    
    ---0---1---2---3---4---5---6---7---8---9----....    (source)
    
    -------0-------1-------2-------3-------4---....     (close)
    
            bufferWhen(()=>close$)
            
    -------(0)-----([1,2])-([3,4])-([5,6])--......  
    */
    
    
    const foo$ = source$.bufferWhen(() => close$);
    
    foo$.subscribe(
      (x) => console.debug("Next: " + x),
      (err) => console.error(err),
      () => console.info("DONE")
    )
    
    /*
    
    "Next: 0"
    "Next: 1,2"
    "Next: 3,4"
    "Next: 5,6"
    "Next: 7,8"
    ...
    */
  • 相关阅读:
    Pytorch tensor求和( tensor.sum())
    Pytorch torch.cat(inputs, dimension=0)
    README.md编写
    Numpy-np.random.normal()正态分布
    numpy数组的分割与合并
    使用Pandas读取CSV文件
    train loss与test loss结果分析/loss不下降
    Python-给数字/字符串前加0
    SVM简单上手示例
    迁移学习简介
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5536682.html
Copyright © 2011-2022 走看看