Rxjs: Chunk and delay stream?

天大地大妈咪最大 提交于 2019-12-12 08:48:55

问题


In short, trying to chunk a really large array into chunks of 10 and wait 5 seconds before emitting the next 10.

Here is what I currently have

Rx.Observable
   .from(hugeArray)
   .bufferCount(10) 
   .delay(5000) //want to wait 5 secs
   .flatMap(e => e) // this needs to go after to flatten the array, buffer spits out arrays of entries
   .flatMap( (data, index) => Rx.Observable.create(observer => {
       // going to render stuff here
       observer.onNext(data)
       observer.onCompleted();  

   }))
   .subscribe(val => console.log('Buffered Values:', val));

Just trying to do 10 chunks ever 5 seconds, only been able to do an initial delay and then it emitted the rest.


回答1:


Your chain just emitted everything at once and then scheduled each chunk to wait 5s starting at the same time so the delay elapsed for all chunks at the exact same moment.

Solution could be to use concatMap() that subscribes to each Observable one by one.

Rx.Observable
    .from(hugeArray)
    .bufferCount(10)
    .concatMap(data => Rx.Observable.of(data).delay(5000))
    .flatMap(e => e) // or mergeAll() or concatAll()
    .flatMap( (data, index) => Rx.Observable.create(observer => {
        // going to render stuff here
        observer.onNext(data);
        observer.onCompleted();
    }))
    .subscribe(val => console.log('Buffered Values:', val));


来源:https://stackoverflow.com/questions/41403500/rxjs-chunk-and-delay-stream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!