RxJS5 emit array items over time and repeat forever

前端 未结 3 1022
情深已故
情深已故 2021-01-06 15:57

I want to emit array items over time (a one second interval between each emit) and when all items have been emitted, repeat over and over.

I know how to do this, but

相关标签:
3条回答
  • 2021-01-06 16:36
    Observable.interval(1000).map(i => MY_ARRAY[i % MY_ARRAY.length])
    
    0 讨论(0)
  • 2021-01-06 16:49

    zip is the go-to operator here, but using Observable.from dumps all values on subscribe, and requires zip to keep a copy of the values. This isn't ideal for large arrays. The original IEnumerable<T> overload for zip is implemented as zipIterable.

    const MY_ARRAY = ['one','two','three'];
    Rx.Observable.interval(1000).zipIterable(MY_ARRAY, (_, v) => v).subscribe(v => console.log(v))
    
    0 讨论(0)
  • 2021-01-06 16:59

    You can use the zip operator:

    const interval$ = Rx.Observable.interval(1000);
    const items$ = Rx.Observable.from([1,2,3]);
    
    const itemsOverTime$ = interval$.zip(items$).repeat();
    
    
    itemsOverTime$.subscribe(([time, val]) => {
      console.log(val);
      // 1
      // 2
      // 3
      // 1
      // 2
      // 3
    });
    
    0 讨论(0)
提交回复
热议问题