首页 > 解决方案 > 可观察:在添加新项目后处理数组中的项目而不重新开始

问题描述

我有一系列搅拌,在一段时间后我对每个项目按顺序执行一些操作。我想要的是,每当我向数组添加(或删除)Item 时,元素必须自己“排队”并等待轮到它被处理。但是我现在得到的是,每当我向数组添加(或删除)Item 时,操作都会从数组的第一项开始。这是我的代码的存根:

private _items = new Subject<string[]>();
private items: string[]=[];

calledMethodOnAction(){
   this._items.pipe(concatAll())
     .pipe(
      concatMap(r => of(this.processingItem(r))
         .pipe(delay(5000)))
    ).subscribe();
}

processingItem(r){
   //Process data
}


addItem(newItem:string){
    const index = this.items.findIndex(f => f.item === newItem);
    if (index <= -1) {items.push(newItem);}
    this._items.next(Object.assign([],items))
}

removeProcessedItem(item){
      const index = this.items.findIndex(f => f.item === item);
      if (index > -1) {
        this.items.splice(index, 1);
      }
      this._items.next(Object.assign([],items))
}

我是 RxJs 的新手。请让我知道我在这里做错了什么。

谢谢!

标签: angulartypescriptrxjs

解决方案


推荐阅读