Reputation: 834
I need to catch the event simultaneously pressing the left and right mouse buttons with RxJS. More precisely, you need to catch the mouseup event with the right and left mouse buttons with a maximum difference of 300 ms.
Upvotes: 1
Views: 366
Reputation: 834
On the advice of Tom Cumming
import { fromEvent, combineLatest } from 'rxjs'
import { filter, map } from 'rxjs/operators'
const upLeft$ = fromEvent(document, 'mouseup').pipe(filter((e) => e.which === 1), map(()=>Date.now()))
const upRight$ = fromEvent(document, 'mouseup').pipe(filter((e) => e.which === 3), map(()=>Date.now()))
const combined$ = combineLatest(upLeft$ , upRight$).pipe(filter((e)=> (e[1]-e[0])<=300 && (e[1]-e[0])>0), map((e)=> (e[1]-e[0])));
combined$.subscribe(() => {
console.log("It work!!!")
});
Upvotes: 0
Reputation: 1168
Date.now()
combineLatest
the two streamsfilter
the values which are more than 300ms apartUpvotes: 2