ng-inactivity-timer
TypeScript icon, indicating that this package has built-in type declarations

0.7.1 • Public • Published

ng-inactivity-timer

Description

ng-inactivity-timer provides a service that keeps track of user activity based on custom activity monitors.

Installation

Install the package by running npm install ng-inactivity-timer

Usage

Provide a configuration for the service (numbers are in seconds):

{
  provideINACTIVITY_CONFIG,
  useValue: <InactivityConfig>{
    inactivityTime900,
    warningTime120
  }
}

Create a custom activity monitor:

export class ActivityMonitorService implements ActivityMonitor {
  public getMonitor() {
    return merge(
      // Add any events you would like to monitor.
      // These events are just examples.
      fromEvent(document, 'keyup'),
      fromEvent(document, 'keydown'),
      fromEvent(document, 'keypress'),
      fromEvent(document, 'mousemove'),
      fromEvent(document, 'click'),
      fromEvent(document, 'mousescroll'),
      fromEvent(document, 'mouseup'),
      fromEvent(document, 'mousedown')
    ).pipe(mapTo(undefined));
  }
  constructor() {}
}

Provide the monitor:

{
  provideACTIVITY_MONITOR,
  useClassActivityMonitorService
}

Inject the inactivity service

export class AppComponent {
  constructor(private inactivityTimerService: InactivityTimerService) {}
}

Using the service

// Start monitoring
// if called with true, will also trigger actvivity
this.inactivityTimerService.startMonitor();
 
// Stop monitoring
this.inactivityTimerService.stopMonitor();
 
// Get an observable emitting Timout objects describing the activity status
this.inactivityTimerService.getTimeOut().subscribe(activity => {
  // do something
});
 
// Trigger an activity (other than the ones provided by the monitor)
this.inactivityTimerService.activate();

Using the Timout objects

The getTimeOut() function emits objects of the Timeout interface:

export interface Timeout {
  showWarning: boolean;
  timedOut: boolean;
  timeLeft: number;
}

Contribute

Package Sidebar

Install

npm i ng-inactivity-timer

Weekly Downloads

2

Version

0.7.1

License

MIT

Unpacked Size

140 kB

Total Files

34

Last publish

Collaborators

  • phl3x0r