Skip to content

Commit

Permalink
refactor: idle detection
Browse files Browse the repository at this point in the history
  • Loading branch information
wghglory committed Sep 27, 2024
1 parent f28f466 commit 1f170d5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ To experience the full potential of `ngx-lift` and `clr-lift`, visit our live si
- **Operator Support:** The project includes a range of operators that can be seamlessly integrated into your Angular
applications. These operators aim to simplify complex operations and promote cleaner, more maintainable code.

- **Component Repository:** `ngx-lift` comes bundled with reusable components. These components are crafted with the best
practices in mind, allowing you to integrate them into your projects and focus effortlessly on building outstanding user
interfaces.
- **Component Repository:** `ngx-lift` comes bundled with reusable components. These components are crafted with the
best practices in mind, allowing you to integrate them into your projects and focus effortlessly on building
outstanding user interfaces.

## Why?

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ChangeDetectionStrategy, Component} from '@angular/core';
import {ChangeDetectionStrategy, Component, OnDestroy, OnInit} from '@angular/core';
import {CalloutComponent, IdleDetectionComponent, PageContainerComponent} from 'clr-lift';
import {IdleDetectionService} from 'ngx-lift';

import {CodeBlockComponent} from '../../../../shared/components/code-block/code-block.component';
import {highlight} from '../../../../shared/utils/highlight.util';
Expand All @@ -11,7 +12,7 @@ import {highlight} from '../../../../shared/utils/highlight.util';
templateUrl: './idle-detection-demo.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class IdleDetectionDemoComponent {
export class IdleDetectionDemoComponent implements OnInit, OnDestroy {
configCode = highlight(`
export class IdleDetectionConfig {
idleDurationInSeconds?: number;
Expand Down Expand Up @@ -108,7 +109,8 @@ export class AppComponent {
}
`);

/*
constructor(private idleDetectionService: IdleDetectionService) {}

ngOnInit() {
this.idleDetectionService.setConfig({
idleDurationInSeconds: 5,
Expand All @@ -135,5 +137,4 @@ export class AppComponent {
ngOnDestroy() {
this.idleDetectionService.clearTimers();
}
*/
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { makeEnvironmentProviders } from '@angular/core';
import {makeEnvironmentProviders} from '@angular/core';

export class IdleDetectionConfig {
idleDurationInSeconds?: number;
timeoutDurationInSeconds?: number;
}

export function provideIdleDetectionConfig(config: IdleDetectionConfig) {
return makeEnvironmentProviders([{ provide: IdleDetectionConfig, useValue: config }]);
return makeEnvironmentProviders([{provide: IdleDetectionConfig, useValue: config}]);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ModuleWithProviders, NgModule } from '@angular/core';
import {ModuleWithProviders, NgModule} from '@angular/core';

import { IdleDetectionConfig, provideIdleDetectionConfig } from './idle-detection.config';
import {IdleDetectionConfig, provideIdleDetectionConfig} from './idle-detection.config';

/**
* Idle detection module.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ export class IdleDetectionService {
* @private
*/
private setupInterruptionEvents() {
this.interruptionSubscription?.unsubscribe();

const debouncedInterruptionEvents = this.interruptionEvents.map((eventName) =>
fromEvent(document, eventName).pipe(throttleTime(1000)),
);
this.interruptionSubscription = merge(...debouncedInterruptionEvents).subscribe(() => this.resetTimer());
if (!this.interruptionSubscription) {
const throttledInterruptionEvents = this.interruptionEvents.map((eventName) =>
fromEvent(document, eventName).pipe(throttleTime(1000)),
);
this.interruptionSubscription = merge(...throttledInterruptionEvents).subscribe(() => this.resetTimer());
}
}

/**
Expand Down Expand Up @@ -200,6 +200,7 @@ export class IdleDetectionService {
clearInterval(this.countdownTimer);

this.interruptionSubscription?.unsubscribe();
this.interruptionSubscription = undefined;
}

/**
Expand Down

0 comments on commit 1f170d5

Please sign in to comment.