|> switchMap

import { switchMap } from '@hullo/core'

Use this operator in case you want to release multiple values on every source one. On every message of a source Observable, transforming function is expected to return an Observable that will contain values that'll be pushed through resulting Observable.

Unlike flatMap, a new outputted Observable is acknowledged immediately and sending new Observable will cancel reading the previous one.

import { Observable, switchMap } from '@hullo/core';
let i = 0;
new Observable(observer => {
setInterval(() => {
observer.next({ i: i++, t: Date.now() });
}, 1000);
})
.pipe(switchMap(
({ i, t }) => observable(observer => {
setInterval(() => {
observer.next(`${i} ${Date.now() - t}`);
}, 100);
})
))
.subscribe({
next: v => { console.log(v) }
})
/* Output
0 107
0 210
0 314
0 420
0 524
0 626
0 726
0 832
0 934
1 106
1 211
1 315
1 420
1 524
1 629
1 732
1 837
1 940
2 106
2 208
2 313
2 418
2 520
2 626
2 726
2 832
2 933
3 105
3 205
3 307
3 408
3 509
3 609
3 710
3 816
3 917
*/

In this example the time difference between Date.now() in source and Date.now in switchMap transformation function will never be greater than a second because that is the time source needs to push a new value which will then cause creation of next comparing times Observable.