Skip to content

Instantly share code, notes, and snippets.

@ken107
Last active December 16, 2024 20:28
Show Gist options
  • Save ken107/3ccd2048bdde45fc9e5e to your computer and use it in GitHub Desktop.
Save ken107/3ccd2048bdde45fc9e5e to your computer and use it in GitHub Desktop.
Return an boolean observable reflecting the user idle state
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