zoukankan      html  css  js  c++  java
  • [RxJS] Implement RxJS `concatMap` by Waiting for Inner Subscriptions to Complete

    Unlike mergeMap and switchMapconcatMap focuses on when "inner" subscriptions "complete" by using a "buffer". Each time concatMap receives a value, it adds each value to a "buffer", waits for previous "inner" subscription to complete, then invokes next with the next value from the "buffer".

    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: () => {
              if (this.buffer.length) {
                const { first, ...rest } = this.buffer;
                this.buffer = rest;
                this._next(first);
              }
            }
          });
        }
      }
    }

  • 相关阅读:
    Redis 持久化
    Redis 事务
    select poll和 epoll
    jdk信任证书
    Java中的锁分类
    mysql触发器同步远程服务器上数据库
    正则表达式
    mysql主从同步
    MySQL逗号分割字段的行列转换技巧
    Mysql中文排序
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9721702.html
Copyright © 2011-2022 走看看