I need the help in the following vue-rx / RxJs question.
I have to make a subscription in vue-rx that watches a props value, and when it is true then it calls a http request in every 500 ms, and stops it when it is false, or when the returned value is 'COMPLETED'.
I tried something like this:
export default {
props: ['started'],
subscriptions() {
return {
currentHttpState: this.$watchAsObservable('started')
.pipe(pluck('newValue'),filter(value => value === false))
.switchMap(() => interval(500).pipe(switchMap(() => this.callHttpRequest()),distinctUntilChanged())),
Thank you for the help!
I'm not too familiar with vue (or vue-rx), so this may just be half the answer (the RxJS bit).
I'm assuming
this.$watchAsObservable('started')withpluck('newValue')is a stream oftrueandfalse? (reflecting the value of thestartedprop)If so, I would use switchMap to switch between an interval/timer and nothing.
that second switchMap will have the effect that if a call takes over 500ms to complete, it will be dropped and you'll never see the results. This also unsubscribed if the
takeWhile()condition isn't met - so you'll have to change that to meet your specific requirements.