RxGo icon indicating copy to clipboard operation
RxGo copied to clipboard

SwitchMap operator

Open karneges opened this issue 3 years ago • 0 comments

Hello! One of the best RX operators is SwitchMap, it looks like FlatMap, but this operator stops previous observable https://rxjs.dev/api/operators/switchMap

It is imperative to be able to complete the previous observable docs: Returns an Observable that emits items based on applying a function that you supply to each item emitted by the source Observable, where that function returns an (so-called "inner") Observable. Each time it observes one of these inner Observables, the output Observable begins emitting the items emitted by that inner Observable. When a new inner Observable is emitted, switchMap stops emitting items from the earlier-emitted inner Observable and begins emitting items from the new one. It continues to behave like this for subsequent inner Observables.

Or maybe you can provide some advice? Example case:

func main() {
	observable := rxgo.Just(1, 2, 3)().FlatMap(func(i rxgo.Item) rxgo.Observable {
		println("new item", i.V.(int))
		return rxgo.Interval(rxgo.WithDuration(time.Second * 1))
	})
	for el := range observable.Observe() {
		println(el.V.(int))
	}
}

I want to make a new observable each time after Just or other Observable creators emit a new value. In this case nice to unsubscribe from interval and create new observable

P.S Well, as I can see the flat map operator doesn't work too output

new item 1
0
1
2
3
4
5
6

karneges avatar Apr 25 '22 17:04 karneges