idle-service
This version requires RxJS v6, if you want to use RxJS v5 check the RxJS 5 branch.
Description
Some applications may need to detect if a user is idle and perform certain actions when this happens like warning them about this inactivity or logging them out of the application.
Installation
You can install the module via npm install @kurtz1993/idle-service
or yarn add @kurtz1993/idle-service
.
Usage
import idleService, { IdleEvents } from '@kurtz1993/idle-service';
idleService.configure({
timeToIdle: 10,
timeToTimeout: 5,
autoResume: true,
listenFor: 'click mousemove',
});
idleService.on(IdleEvents.UserIsBack, () => {
console.log('User is back!');
});
idleService.on(IdleEvents.UserHasTimedOut, () => {
console.log('User has timed out!');
});
idleService.on(IdleEvents.TimeoutWarning, countdown => {
console.log(`User has ${countdown} seconds to come back!`);
});
idleService.on(IdleEvents.UserIsIdle, () => {
console.log('User has become idle!');
});
idleService.on(IdleEvents.UserIsActive, () => {
console.log('User is active');
});
idleService.start();
Configuration
You can configure the service to change the default timers and other options by calling the configure method.
import idleService from 'idle-service';
idleService.configure({
timeToIdle: 10,
timeToTimeout: 5,
autoResume: true,
listenFor: 'click mousemove',
});
Events
These are the events available within the IdleService:
enum IdleEvents {
UserIsActive
UserIsIdle
UserIsBack
TimeoutWarning
UserHasTimedOut
}
API Reference
class IdleOptions {
timeToIdle: number;
timeToTimeout: number;
autoResume: boolean;
listenFor: string;
}
class IdleService {
userState: UserState;
configure(options: Partial<IdleOptions>): void;
start(): void;
stop(timedOut?: boolean): void;
reset: () => void;
startTimeoutCountdown(): void;
on(eventType: IdleEvents, action: (value: any) => void): Subscription;
class UserState {
isIdle: boolean;
hasTimedout: boolean;
userInactivityTime: number;
}