combineLatest but remove completed observables

Viewed 66

How can I combine the latest values but remove the completed observables dynamically? The combineLatest operator does not remove completed observables, it just repeats the last value they emitted until all observables complete.

I wanted something like this:

combineLatestAlive([interval(100).pipe(take(2)), interval(100)]).subscribe(console.log)

//[0,0]
//[1,0]
//[1,1]
//[1]
//[2] ... and so on

1 Answers

I added two operators.

  1. endWith: Append end mark like undefined
  2. map: Transform array
combineLatest([interval(100).pipe(take(2), endWith(undefined)), interval(100)]).pipe(
    map(values => values.filter(v => v !== undefined))
  ).subscribe(console.log);

result is: (your wanted result has [1, 1] but [1, is already completed value.)

[ 0, 0 ]
[ 1, 0 ]
[ 0 ]
[ 1 ]
[ 2 ]
...
Related