简体   繁体   English

当另一个Observable发出时,如何停止发出的值?

[英]How to stop an emited value when another Observable emits?

Having two Observables, one emits a mouseover event (debounced by 500ms) and the other one a mouseout event, I'm looking for a possibility to stop the first Observable (mouseover) from emiting when the second Observable (mouseout) occurs. 有两个Observable,一个发出鼠标悬停事件(由500毫秒去抖动),另一个发出鼠标悬停事件,我正在寻找一种可能性,当第二个Observable(鼠标悬停)发生时,第一个Observable(鼠标悬停)停止发射。

let mouseOutObservable = Observable.fromEvent($('.row'), 'mouseout')

Observable.fromEvent($('.row'), 'mouseover')
          .debounceTime(500)
          // .stopEmitingWhen(mouseOutObservable) --> how? possible?
          .subscribe(event => {
              // show tooltip
              mouseOutObservable.first()
                                .subscribe(() => {
                                   // destroy tooltip
                                });
          });

takeUntil正是您想要的。

Matt Burnell's and Ivan Malagon's suggested solutions work fine if no neighbouring elements. 如果没有相邻元素,则马特·伯内尔(Matt Burnell)和伊万·马拉贡(Ivan Malagon)提出的解决方案可以很好地工作。 But my row elements do occur within a table. 但是我的行元素确实出现在表中。 I did write my question kinda interpretable. 我确实写了我的问题可以解释。 Applying their code suggestion will unsubscribe/dispose the subscription completely but I do need a solution to stop only current emited value from arriving in subscribe. 应用他们的代码建议将完全取消订阅/处置订阅,但是我确实需要一种解决方案以仅停止当前发出的值到达订阅。

However, both answers do solve my question above. 但是,两个答案都确实解决了我上面的问题。 ;-) Therefore I accepted Matt Burnell's short answer. ;-)因此,我接受了Matt Burnell的简短回答。

In order to include my additional requirement, I came up with another solution which merges both observable to one, followed by using a debounce time and continue only if the last event is a mouseover event. 为了包括我的其他要求, 我想出了另一种解决方案,将可观察到的两个合并为一个,然后使用去抖动时间,并且仅当最后一个事件是鼠标悬停事件时才继续。

    Observable.fromEvent($('.row'), 'mouseover')
              .merge(mouseOutObservable)
              .debounceTime(500)
              .filter(event => event[ 'type' ] === 'mouseover')
              .subscribe(event => {
                  // ....
              });

You can get the subscription object for the mouseover event and then dispose that subscription within the mouseout function. 您可以获取mouseover事件的预订对象,然后将该预订放置在mouseout函数中。

 let mouseOutObservable = Rx.Observable.fromEvent($('.row'), 'mouseout') let mouseOverObservable = Rx.Observable.fromEvent($('.row'), 'mouseover') .debounce(500); let mouseOverObservableSubscription = mouseOverObservable.subscribe(() => { $('#output').append('<p>mouseover</p>'); }); mouseOutObservable.subscribe(() => { $('#output').append('<p>mouseout</p>'); mouseOverObservableSubscription.dispose(); }) 
 .row { min-height: 48px; background-color: orange; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.6/rx.all.js"></script> <div class="row">Mouse over me!</div> <div id="output"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM