https://kotlinlang.org logo
#rx
Title
u

ursus

04/18/2018, 3:11 PM
id like to ask, what do you think about that jakewhartons @jw rx state talk, where you should map observables so started/success/progress/error events, do you model every observable into this? Not only ones close to subscribe? What am I seeing its kind of difficult / ugly to flatmap these kinds of mapped observables, since you then need to look into emission exact type in order to map it into parent observable types like this. Since it was say basically singles before, now you need to filter for Started emit, etc.
Copy code
-- Basic usecase -- 
fooManager.fooObservable()
	.someOtherObservable()  
	.flatMap(other -> barManager.barObservable())
	.subscribe(...)

-- Bar is now mapped to events --
fun barObservable() : Observable<BarEvent> {
	whatEverObservable()
	.map { bar -> BarEvent.Success(bar) }
	.onErrorReturn { t -> BarEvent.Error(t) }
	.startWith(BarEvent.Started())
}


-- Foo want to be mapped aswell, not looks like this
fun fooObservable() : Observable<FooEvent> {
	.someOtherObservable()  
	.flatMap { other -> barManager.barObservable() }
        // this is gross
	.filter { barEvent -> barEvent is not BarEvent.Started }
	.map { barEvent -> 
			if barEvent is BarEvent.Error {
				FooEvent.Error(barEvent.t)
			} else {
				FooEvent.Success(barEvent.bar)			
			}
		})
        //
  	// .map { _ -> FooEvent.Success() }
	.onErrorReturn { t -> FooEvent.Error(t) }
	.startWith(FooEvent.Started())
	.subscribe(...)
}