Example RxJS Observable when mouse or click activity Re-starts

爷,独闯天下 提交于 2019-12-22 01:30:31

问题


I'm able to create 2 observables to watch mouse move and click events as such:

var mousemove$ = Rx.Observable.fromEvent(document, 'mousemove');
var click$ = Rx.Observable.fromEvent(document, 'click');

I'm also able to use merge() and debounceTime() to wait for either mousemove or click to not happen for 10 seconds like this:

var allactivity$ = Rx.Observable.merge(
    mousemove$.mapTo('Mouse!'),
    click$.mapTo('Click!')
  );

var lastact$ = allactivity$.debounceTime(10000);

However, I would like to somehow construct an observable for when a user Re-starts either moving the mouse or clicking after this 10 second debounceTime() limit.

Could someone help me construct this Observable? I think I'm missing something simple.


回答1:


You could use something like this:

var restart$ = Rx.Observable.of('Kick off')
  .merge(lastact$)
  .mergeMap(() => allactivity$.skipUntil(lastact$).first());

Explanation

  • Rx.Observable.of('Kick off') - Once in the beginning, ...
  • .merge(lastact$) - and for everytime lastact$ emits, ...
  • .mergeMap(() => ...) - create an observable ...
  • allactivity$ - that will observe on allactivity$ for all items...
  • .skipUntil(lastact$) - since the firstlastact$ emission (after the creation of this observable) ...
  • .first() - and take only the first item (that is, the first activity happen after the lastact$ emit)

Edit:

The above observable will not trigger on the first mouse move, to handle that:

var firstMoveAndRestart$ = restart$.merge(allactivity$.first());


来源:https://stackoverflow.com/questions/48144202/example-rxjs-observable-when-mouse-or-click-activity-re-starts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!