zoukankan      html  css  js  c++  java
  • [RxJS] `add` Inner Subscriptions to Outer Subscribers to `unsubscribe` in RxJS

    When subscribers create new "inner" sources and subscriptions, you run the risk of losing control of them when the outer subscriber unsubscribes. This is handled easily enough if you use the add method from the Subscriber class to add the "inner" subscription to the Subscriber.

    class MyConcatMapSubscriber extends Subscriber {
      innerSubscription
      buffer = []
    
      constructor(sub, fn) {
        super(sub)
    
        this.fn = fn
      }
    
      _next(value) {
        const { isStopped } = this.innerSubscription || {
          isStopped: true
        }
    
        if (!isStopped) {
          this.buffer = [...this.buffer, value]
        } else {
          const o$ = this.fn(value)
    
          this.innerSubscription = o$.subscribe({
            next: value => {
              this.destination.next(value)
            },
            complete: () => {
              console.log(this.buffer)
              if (this.buffer.length) {
                const [first, ...rest] = this.buffer
                this.buffer = rest
                this._next(first)
              }
            }
          })
    
          // to tell when the outter subscription complete, the inner subscription should also completed
          this.add(this.innerSubscription)
        }
      }
    }

  • 相关阅读:
    VSCode使用笔记
    python调用C++
    ubuntu下编译C++程序
    使用swig在python中调用C++
    VSCode调试data层时自身的一个bug
    MNN配置
    金融业务中的命名惯例
    Clang的线程安全分析静态工具
    gdb命名记录
    开发小结-产品类
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9721703.html
Copyright © 2011-2022 走看看