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 idle-service
or yarn add idle-service
.
Usage
import idleService, { IdleEvents } from 'idle-service';
service.on(IdleEvents.UserIsBack, () => {
console.log('User is back!');
});
service.on(IdleEvents.UserHasTimedOut, () => {
console.log('User has timed out!');
});
service.on(IdleEvents.TimeoutWarning, countdown => {
console.log(`User has ${countdown} seconds to come back!`);
});
service.on(IdleEvents.UserIsIdle, () => {
console.log('User has become idle!');
});
service.on(IdleEvents.UserIsActive, () => {
console.log('User is active');
});
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',
});
API Reference
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 IdleOptions {
timeToIdle: number;
timeToTimeout: number;
autoResume: boolean;
listenFor: string;
}
class UserState {
isIdle: boolean;
hasTimedout: boolean;
userInactivityTime: number;
}
Events
These are the events available within the IdleService:
enum IdleEvents {
UserIsActive
UserIsIdle
UserIsBack
TimeoutWarning
UserHasTimedOut
}