so as I understood from this question here I understood that timeout
operator errors if an observable does not emit any value in the given window of time... the problem for me is, that this window of time resets after each emit, making it necessary to complete the sequence if you are only interested if the first value(s) emit within the window...
is there a nice way to have a "timeout to first"? Other than .take(1).timeout(1000)
?
In addition to @Maxime's answer, you can use race to create a race between your observable's first value and a timeout. We construct the timeout by combining
never
withtimeout
.Thus we end up with a race between your source observable, and an observable that will never emit a value, but will throw an error after time goes by. If your source observable produces its first value, then
race
will stop listening to the timeout observable.There's no timeout for first only as far as I'm aware and here's a way of doing it:
(Plunkr available here)
But, I felt there might be another way so I asked on Gitter and @Dorus gave me a better result:
Explanation here, all credit goes to @Dorus on this one:
publish
has 2 flavours:.publish() : ConnectableObservable<T>
.publish(selector : (src : Observable<T>) => Observable<R>): Observable<R>
.publish()
returns aConnectableObservable
and can be used either by callingrefCount
on it or by callingconnect
on it yourself..publish(selector)
let you use the multicasted source in the selector, and emit the result. By the way,.publish()
is the same as.multicast(() => new Subject())
and.publish(selector)
is the same as.multicast(() => new Subject(), selector)
.Also
.publish().refCount()
is pretty much the same as.share()
. You are correct to callshare
beforepartition
. That solves the problem partially. I still prefer.publish(selector)
because it's safer:.publish(selector)
will run the selector andsubscribe
to all sources before it subscribes to the source observable. With share, the first subscription will activate the source, and anything emitted sync will be missed by the second subscription. This could be as worse as using an total sync source and then the second subscription would rerun the source completely.One last addition:
.publish(selector)
has the same signature as.let(selector)
, butlet
does not multicast the source. That's the difference.