zoukankan      html  css  js  c++  java
  • [RxJS] Use takeUntil instead of manually unsubscribing from Observables

    Manually unsubscribing from subscriptions is safe, but tedious and error-prone. This lesson will teach us about the takeUntil operator and its utility to make unsubscribing automatic.

    const click$ = Rx.Observable.fromEvent(document, 'click');
    
    const sub = click$.subscribe(function(ev) {
      console.log(ev.clientX);
    });
    
    setTimeout(() => {
      sub.unsubscribe();
    }, 2000);

    In the code we manully unsubscribe.

    We can use tha helper methods such as takeUntil, take() help to automatically handle subscritpiton.

    const click$ = Rx.Observable
      .fromEvent(document, 'click');
    
    const four$ = Rx.Observable.interval(4000).take(1);
    
    /*
    click$          --c------c---c-c-----c---c---c-
    four$           -----------------0|
    clickUntilFour$ --c------c---c-c-|
    */
    
    const clickUntilFour$ = click$.takeUntil(four$);
    
    clickUntilFour$.subscribe(function (ev) {
      console.log(ev.clientX);
    });
  • 相关阅读:
    java三种实现线程的方法比较
    java基础
    java闭包
    android 设置textview跑马灯效果
    android控制系统音量
    android 查找某个特定文件后缀名
    android 歌词解析
    textview滑动效果
    puporwindow
    android 网络通讯
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6917947.html
Copyright © 2011-2022 走看看