bearnithi / bn-ng-idle

Angular user idle detector service
28 stars 11 forks source link

Performance suggestion #22

Open rmeshksar opened 3 years ago

rmeshksar commented 3 years ago

Hi, This is very helpful service. I think the following changes will make it even better in term of the performance of the applicaiton using this service:

  constructor(private zone: NgZone) {
  }

  public startWatching(timeOutSeconds): Observable<any> {
    this.zone.runOutsideAngular(() => {
      this.idle$ = merge(
        fromEvent(document, 'mousemove'),
        fromEvent(document, 'click'),
        fromEvent(document, 'mousedown'),
        fromEvent(document, 'keypress'),
        fromEvent(document, 'DOMMouseScroll'),
        fromEvent(document, 'mousewheel'),
        fromEvent(document, 'touchmove'),
        fromEvent(document, 'MSPointerMove'),
        fromEvent(window, 'mousemove'),
        fromEvent(window, 'resize'),
      );

      this.timeOutMilliSeconds = timeOutSeconds * 1000;

      this.idleSubscription = this.idle$
        .pipe(debounceTime(500)) // or maybe any number smaller than the timeout value
        .subscribe((res) => {
          this.resetTimer();
        });

      this.startTimer();

    });
    return this.expired$;
  }
bearnithi commented 2 years ago

Thanks for the suggestion @rmeshksar . I'll update in the next release in this week.