3

The inner merged observable isn't terminating with this code (rxjs 5.5.6):

    let source = new Subject<string[]>();

    // when the source emits a vector of strings, output 
    // each string with a 1s delay
    source.switchMap(v => Observable.from(v)
        .map(s => Observable.of(s).delay(1000).do(s => console.log('do: ' + s)))
        // only one active observable at time  
        .mergeAll(1) 
    ).subscribe(val => console.log('result: ' + val));

    // emit two vectors, 1.5s apart
    Observable.interval(1500).take(2).map(i => ['a' + i, 'b' + i, 'c' + i])
        .subscribe(v => source.next(v));

Output is:

do: a0
result: a0
do: b0
do: a1
result: a1
do: c0
do: b1
result: b1
do: c1
result: c1

The expected output is:

do: a0
result: a0
do: a1
result: a1
do: b1
result: b1
do: c1
result: c1

That is, after the second vector emits, the switchMap should unsubscribe from the observable on the first vector, canceling that observable. And while the unsubscribe is clearly working, the inner observable is still running, as evidence by the "do: a0 .. b0 .. c0" in the output from the first example.

And in fact the expected output is exactly what you get from this code:

    let source = 
        Observable.interval(1500).take(2).map(i => ['a' + i, 'b' + i, 'c' + i]);

    source.switchMap(v => Observable.from(v)
        .map(s => Observable.of(s).delay(1000).do(s => console.log('do: ' + s)))
        .mergeAll(1)
    ).subscribe(val => console.log('result: ' + val));

But why doesn't the first example behave the same way?

kayjtea
  • 2,979
  • 1
  • 20
  • 19
  • 2
    I think you should create an [issue](https://github.com/ReactiveX/rxjs/issues) for this in the RxJS repo. – cartant Feb 15 '18 at 21:54
  • 1
    I've run both sets of code in Chrome using RXJS5.5.6 and I get the expected output in both cases. – Miller Feb 16 '18 at 01:55
  • 1
    Yeah. Running the first snippet in Node and in the Browser (with 5.5.6) I see the expected output, too. I think I can recall seeing a `do`-related bug being fixed, so you might want to double check your version, etc. – cartant Feb 16 '18 at 03:57
  • Now I'm really confused. I can confirm the first snippet produces the expected output using Stack Overflow' s runnable javascript snippets. My real stack is angular-cli/angular 5/typescript -- and there it fails in Chrome. I will double check versions, etc. as suggested. Thanks! – kayjtea Feb 16 '18 at 05:51
  • 1
    I've had the same issue in my Angular app (and only there) and could fix it by changing the import. I did create an issue on rxjs for it. – Ingo Bürk Feb 16 '18 at 06:03
  • 2
    https://github.com/ReactiveX/rxjs/issues/3306 – Ingo Bürk Feb 16 '18 at 06:03

0 Answers0