UNPKG

@omostan/ngx-idle

Version:

A module for responding to idle users in Angular applications. this is a forked version of the original @ng-Idle

147 lines (141 loc) 5.42 kB
import * as i0 from '@angular/core'; import { EventEmitter, Injectable, NgModule } from '@angular/core'; import * as i1 from '@angular/common/http'; import { HttpRequest } from '@angular/common/http'; import * as i1$1 from '@ng-idle/core'; import { KeepaliveSvc, NgIdleModule } from '@ng-idle/core'; /** * An example of an injectable service. */ class Keepalive extends KeepaliveSvc { /* * Initializes a new instance of Keepalive * @param http - The HTTP service. */ constructor(http, zone) { super(); this.http = http; this.zone = zone; this.pingInterval = 10 * 60; /* * An event emitted when the service is pinging. */ this.onPing = new EventEmitter(); /* * An event emitted when the service has pinged an HTTP endpoint and received a response. */ this.onPingResponse = new EventEmitter(); } /* * Sets the string or Request that should be used when pinging. * @param url - The URL or Request object to use when pinging. * @return The current Request used when pinging. */ request(url) { if (typeof url === 'string') { this.pingRequest = new HttpRequest('GET', url); } else if (url instanceof HttpRequest) { this.pingRequest = url; } else if (url === null) { this.pingRequest = null; } return this.pingRequest; } /* * Sets the interval (in seconds) at which the ping operation will occur when start() is called. * @param seconds - The ping interval in seconds. * @return The current interval value. */ interval(seconds) { if (!isNaN(seconds) && seconds > 0) { this.pingInterval = seconds; } else if (!isNaN(seconds) && seconds <= 0) { throw new Error('Interval value must be greater than zero.'); } return this.pingInterval; } /* * Immediately performs the ping operation. If a request has been set, an HTTP * request will be made and the response will be emitted via the * onPingResponse event. */ ping() { this.onPing.emit(null); if (this.pingRequest) { this.http.request(this.pingRequest).subscribe((response) => { this.onPingResponse.emit(response); }, (error) => { this.onPingResponse.emit(error); }); } } /* * Starts pinging on an interval. */ start() { this.stop(); this.zone.runOutsideAngular(() => { this.pingHandle = setInterval(() => { this.zone.run(() => { this.ping(); }); }, this.pingInterval * 1000); }); } /* * Stops pinging on an interval. */ stop() { if (this.hasPingHandle()) { clearInterval(this.pingHandle); this.pingHandle = null; } } /* * Performs any cleanup tasks when Angular destroys the instance. */ ngOnDestroy() { this.stop(); } /* * Returns whether or not the service will ping automatically at the specified interval. * @return True if the service will ping at the specified interval; otherwise, false. */ isRunning() { return this.hasPingHandle(); } hasPingHandle() { return this.pingHandle !== null && typeof this.pingHandle !== 'undefined'; } } Keepalive.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: Keepalive, deps: [{ token: i1.HttpClient }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Injectable }); Keepalive.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: Keepalive }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: Keepalive, decorators: [{ type: Injectable }], ctorParameters: function () { return [{ type: i1.HttpClient }, { type: i0.NgZone }]; } }); class NgIdleKeepaliveModule { static forRoot() { return { ngModule: NgIdleKeepaliveModule, providers: [Keepalive, { provide: KeepaliveSvc, useExisting: Keepalive }] }; } } NgIdleKeepaliveModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: NgIdleKeepaliveModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); NgIdleKeepaliveModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.1.1", ngImport: i0, type: NgIdleKeepaliveModule, imports: [i1$1.NgIdleModule] }); NgIdleKeepaliveModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: NgIdleKeepaliveModule, imports: [NgIdleModule.forRoot()] }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.1", ngImport: i0, type: NgIdleKeepaliveModule, decorators: [{ type: NgModule, args: [{ imports: [NgIdleModule.forRoot()] }] }] }); /* * Public API Surface of keepalive */ /** * Generated bundle index. Do not edit. */ export { Keepalive, NgIdleKeepaliveModule }; //# sourceMappingURL=ng-idle-keepalive.mjs.map