Last active
December 16, 2024 20:28
-
-
Save ken107/3ccd2048bdde45fc9e5e to your computer and use it in GitHub Desktop.
Return an boolean observable reflecting the user idle state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function detectUserIdle(periodOfInactivity) { | |
return rxjs.merge( | |
rxjs.timer(periodOfInactivity), | |
rxjs.merge( | |
rxjs.fromEvent(document, "mousemove"), | |
rxjs.fromEvent(document, "keydown"), | |
rxjs.fromEvent(document, "touchstart") | |
) | |
).pipe( | |
rxjs.switchMap(event => !event ? rxjs.of(true) : rxjs.concat(rxjs.of(false), rxjs.throwError(() => "reset"))), | |
rxjs.retry({delay: 1000}), | |
rxjs.distinctUntilChanged() | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment