RxJs: Incrementally push stream of data to BehaviorSubject<[]>

后端 未结 2 873
無奈伤痛
無奈伤痛 2021-01-04 01:04

Basically I\'m trying to inflate BehaviorSubject<[]> with array of data which will be loaded in chunks.

BehaviorSubject<[]> wil

相关标签:
2条回答
  • 2021-01-04 01:34

    Instead of using concat, you can instead use the spread operator:

    data = new BehaviorSubject<any[]>([]);
    
    addData(foo: any): void {
      this.data.next([...this.data.getValue(), ...foo])
    }
    

    I've found this to be a bit more readable than a straight concat

    0 讨论(0)
  • 2021-01-04 01:37

    You can use getValue() method to achieve what you want to do.

    Example:

    data = new BehaviorSubject<any[]>([]);
    
    addData(foo:any):void{
      // I'm using concat here to avoid using an intermediate array (push doesn't return the result array, concat does).
      this.data.next(this.data.getValue().concat([foo]));
    }
    
    0 讨论(0)
提交回复
热议问题