1
votes

How could I do something like this:

I have an observable that emits some result I load from an API. This is mapped into an ResultObject that holds a state (loading, success, error) and the object or an error (I don't want to emit an error because this observable will update again).

For now, I merge two observables, one just emitting an ResultObject with state loading and the observable that really loads the data and then emits an ResultObject with state success.

Usually, if in a fast network, that loading part is quite fast, so the UI reacts to the loading state, shows that for a few hundred millisecounds and then shows the data. Does not look that nice.

Now I want to show that loading indicator just, if the data loading takes longer than a second.

Before I fix that on UI side, I thought to ask this here:

Is there a possibility, to have an observable, have an operator that does something like this:

myObservable
    .emitAfterTimeout(1000, new ResultObject(LOADING)

The normal timeout operator does not help, because I need to keep the observable active, I want to have the result anyway.

Thanks for your help

You could use a debounce on the Observable. - flakes
@flakes you mean a debounce of 1 second on the merged observable? Hm, but that would also mean, if my result comes very fast, I still have to wait one second for it... - Artur Hellmann
Yup, but If you complete the observable after the success, then the result will be emitted immediately upon completion. - flakes
@flakes Problem is: Good idea... If I flatMap that correctly I will get what I want I think. Will try that. Thank you. - Artur Hellmann
No problem! Good luck! - flakes