David
David

Reputation: 16067

Cancel observable if another sequence emits

I have two observables, hover$ and unhover$, triggered on hover and on blur respectively.

The unhover$ has a debounceTime(500) to delay it from being too fast.

Now consider this timeline:

 [    0ms] hover triggered
 [   50ms] unhover 500ms debounce started
 [  100ms] hover triggered
 [  550ms] unhover triggered 

How can I 'cancel' the first unhover$ that is pending, when a new value is emitted by the hover$ sequence?

Upvotes: 1

Views: 1389

Answers (1)

Mark van Straten
Mark van Straten

Reputation: 9425

You can use takeUntil to have your observable be unsubscribed if a notifier Observable emits a value.

Something like this:

unhover$
.debounceTime(500)
.switchMap(evt => Rx.Observable.of(evt)
  .takeUntil(hover$)
)
.subscribe(console.log);

Upvotes: 4

Related Questions