Here's my take on this:
const mouseMove$ = fromEvent<MouseEvent>(document, 'mousemove');
const keyPress$ = fromEvent<KeyboardEvent>(document, 'keyup');
const mergeBothEvents = merge(keyPress$, mouseMove$);
const interval$ = timer(60000);
mergeBothEvents
.pipe(
startWith('initial'),
switchMap(() => {
return interval$;
})
)
.subscribe(() => {
console.log('60 seconds past');
console.log('Perform function here');
});
A bit of inside into what's happening in the code.
fromEvent<MouseEvent>
used to capture the mouse movements.
fromEvent<KeyboardEvent>
used to capture the keyboard key up click event.
merge(keyPress$, mouse$)
is used to merge both the observables so that they can run in parallel.
timer(60000)
is used to check the time of a minute before running the required function.
In mergeBothEvents
we have used to more operators startWith()
and switchMap
startWith()
is used for the edge-case in which if the user doesn't do anything(mouse or keyboard) when he enters the page the mergeBothEvents
event won't be emitted. Thanks @MrkSef to for pointing that out.
Finally switchMap
is used to cancel the previous subscription of the event when the user moves the mouse or presses the keyboard so the timer restarts from 0th second.