UNPKG

@c8y/ngx-components

Version:

Angular modules for Cumulocity IoT applications

96 lines (90 loc) 4.79 kB
import { CommonModule } from '@angular/common'; import * as i0 from '@angular/core'; import { Injectable, NgModule } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import * as i1 from '@c8y/ngx-components/api'; import * as i2 from '@c8y/ngx-components'; import { AlertService, gettext } from '@c8y/ngx-components'; import { NEVER, combineLatest } from 'rxjs'; import { share, timeout, catchError, startWith, map, take } from 'rxjs/operators'; class PendingMoRequestInterceptor { constructor(injector) { this.injector = injector; } intercept(req, next) { const alertService = this.injector.get(AlertService); const timeoutValue = 5_000; if (/inventory\/managedObjects/.test(req.url)) { const response$ = next.handle(req).pipe(share()); const timeout$ = response$.pipe(timeout({ first: timeoutValue }), catchError(error => { if (error.name === 'TimeoutError') { alertService.add({ type: 'system', text: gettext('The request is taking longer than usual. We apologize for the inconvenience.'), timeout: 10000 }); } return NEVER; }), startWith('')); return combineLatest([response$, timeout$]).pipe(map(([response]) => response), take(1)); } else { return next.handle(req); } } } class PendingMoRequestService { constructor(injector, apiService, alertService, destroyRef) { this.injector = injector; this.apiService = apiService; this.alertService = alertService; this.destroyRef = destroyRef; } setupHooks() { this.apiService.addInterceptor(new PendingMoRequestInterceptor(this.injector)); /* Backend will return 202 when a request takes more than 30 seconds to complete * and will continue the execution in the background. So the user will be able to navigate away. */ this.apiService .hookResponse(call => call.method === 'DELETE' && /inventory\/managedObjects/.test(call.url) && call.response.status === 202) .pipe(takeUntilDestroyed(this.destroyRef)) .subscribe(() => { this.alertService.add({ type: 'system', text: gettext('The managed object is deleted in the background. Continue with using the platform as normal.'), timeout: 10000 }); }); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: PendingMoRequestService, deps: [{ token: i0.Injector }, { token: i1.ApiService }, { token: i2.AlertService }, { token: i0.DestroyRef }], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: PendingMoRequestService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: PendingMoRequestService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: () => [{ type: i0.Injector }, { type: i1.ApiService }, { type: i2.AlertService }, { type: i0.DestroyRef }] }); class PendingMoRequestModule { constructor(pendingMoRequestService) { this.pendingMoRequestService = pendingMoRequestService; this.pendingMoRequestService.setupHooks(); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: PendingMoRequestModule, deps: [{ token: PendingMoRequestService }], target: i0.ɵɵFactoryTarget.NgModule }); } static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.13", ngImport: i0, type: PendingMoRequestModule, imports: [CommonModule] }); } static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: PendingMoRequestModule, providers: [PendingMoRequestService], imports: [CommonModule] }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: PendingMoRequestModule, decorators: [{ type: NgModule, args: [{ imports: [CommonModule], providers: [PendingMoRequestService] }] }], ctorParameters: () => [{ type: PendingMoRequestService }] }); /** * Generated bundle index. Do not edit. */ export { PendingMoRequestModule }; //# sourceMappingURL=c8y-ngx-components-pending-mo-request.mjs.map