UNPKG

@anexia/registry-loading-interceptor

Version:

This library provides an interceptor which maps Http Requests to a Loading State.

181 lines (167 loc) 8.3 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common/http'), require('rxjs'), require('rxjs/operators')) : typeof define === 'function' && define.amd ? define('@anexia/registry-loading-interceptor', ['exports', '@angular/core', '@angular/common/http', 'rxjs', 'rxjs/operators'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.anexia = global.anexia || {}, global.anexia['registry-loading-interceptor'] = {}), global.ng.core, global.ng.common.http, global.rxjs, global.rxjs.operators)); }(this, (function (exports, i0, http, rxjs, operators) { 'use strict'; function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n['default'] = e; return Object.freeze(n); } var i0__namespace = /*#__PURE__*/_interopNamespace(i0); var REQUEST_ID_GENERATOR = new i0.InjectionToken('request.id.generator.strategy'); var REQUEST_FILTER = new i0.InjectionToken('request.filter.strategy'); var UrlFragmentIdGenerator = /** @class */ (function () { function UrlFragmentIdGenerator() { } UrlFragmentIdGenerator.prototype.getIdentifier = function (fragment) { return String(fragment).replace(/[:\/&?]/g, '_').trim(); }; UrlFragmentIdGenerator.prototype.createId = function (request) { return this.getIdentifier(request.url); }; return UrlFragmentIdGenerator; }()); var NoRequestFiltering = /** @class */ (function () { function NoRequestFiltering() { } NoRequestFiltering.prototype.exclude = function (request) { return false; }; return NoRequestFiltering; }()); var RegistryLoadingInterceptor = /** @class */ (function () { function RegistryLoadingInterceptor(requestIdGenerator, requestFilter) { var _this = this; this.requestIdGenerator = requestIdGenerator; this.requestFilter = requestFilter; this.loadingDictionary = new Map(); this.stateChangeTrigger$ = new rxjs.BehaviorSubject(false); this.networkRequestCounter$ = new rxjs.BehaviorSubject(0); this.loadingState$ = this.stateChangeTrigger$ .asObservable() .pipe(operators.switchMap(function () { return rxjs.of(_this.loadingDictionary); })); this.isAnyRequestLoading$ = this.networkRequestCounter$ .asObservable() .pipe(operators.map(function (count) { return count > 0; })); } RegistryLoadingInterceptor.mapEventTypeToLoading = function (event) { return (event === null || event === void 0 ? void 0 : event.type) === 0; }; RegistryLoadingInterceptor.prototype.intercept = function (request, delegate) { var _this = this; var requestCancelled = true; return delegate.handle(request) .pipe(operators.map(function (event) { if ((event === null || event === void 0 ? void 0 : event.type) === 4) { requestCancelled = false; } return event; }), this.mapHttpEventToLoadingCount$(), this.mapHttpEventToLoadingState$(request), operators.catchError(function (error) { requestCancelled = false; _this.updateLoadingCounter(false); if (!_this.requestFilter.exclude(request)) { _this.updateLoadingState(_this.getRequestId(request), false); } return rxjs.throwError(error); }), operators.finalize(function () { if (requestCancelled) { _this.decrementCounter(); } })); }; RegistryLoadingInterceptor.prototype.mapHttpEventToLoadingState$ = function (request) { var _this = this; if (this.requestFilter.exclude(request)) { return operators.tap(); } return operators.tap(function (event) { return _this.updateLoadingState(_this.getRequestId(request), RegistryLoadingInterceptor.mapEventTypeToLoading(event)); }); }; RegistryLoadingInterceptor.prototype.mapHttpEventToLoadingCount$ = function () { var _this = this; return operators.tap(function (event) { return _this.updateLoadingCounter(RegistryLoadingInterceptor.mapEventTypeToLoading(event)); }); }; RegistryLoadingInterceptor.prototype.updateLoadingState = function (requestId, isLoading) { this.loadingDictionary.set(requestId, isLoading); this.stateChangeTrigger$.next(true); }; RegistryLoadingInterceptor.prototype.updateLoadingCounter = function (isLoading) { isLoading ? this.incrementCounter() : this.decrementCounter(); }; RegistryLoadingInterceptor.prototype.getRequestId = function (request) { return this.requestIdGenerator.createId(request); }; RegistryLoadingInterceptor.prototype.incrementCounter = function () { this.networkRequestCounter$.next(this.networkRequestCounter$.value + 1); }; RegistryLoadingInterceptor.prototype.decrementCounter = function () { this.networkRequestCounter$.next(this.networkRequestCounter$.value - 1); }; return RegistryLoadingInterceptor; }()); RegistryLoadingInterceptor.ɵprov = i0__namespace.ɵɵdefineInjectable({ factory: function RegistryLoadingInterceptor_Factory() { return new RegistryLoadingInterceptor(i0__namespace.ɵɵinject(REQUEST_ID_GENERATOR), i0__namespace.ɵɵinject(REQUEST_FILTER)); }, token: RegistryLoadingInterceptor, providedIn: "root" }); RegistryLoadingInterceptor.decorators = [ { type: i0.Injectable, args: [{ providedIn: 'root' },] } ]; RegistryLoadingInterceptor.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: i0.Inject, args: [REQUEST_ID_GENERATOR,] }] }, { type: undefined, decorators: [{ type: i0.Inject, args: [REQUEST_FILTER,] }] } ]; }; var defaultProviders = [ { provide: REQUEST_ID_GENERATOR, useClass: UrlFragmentIdGenerator, }, { provide: REQUEST_FILTER, useClass: NoRequestFiltering }, { provide: http.HTTP_INTERCEPTORS, useExisting: RegistryLoadingInterceptor, multi: true } ]; var RegistryLoadingInterceptorModule = /** @class */ (function () { function RegistryLoadingInterceptorModule() { } return RegistryLoadingInterceptorModule; }()); RegistryLoadingInterceptorModule.decorators = [ { type: i0.NgModule, args: [{ imports: [ http.HttpClientModule ], providers: [defaultProviders] },] } ]; /* * Public API Surface of registry-loading-interceptor */ /** * Generated bundle index. Do not edit. */ exports.NoRequestFiltering = NoRequestFiltering; exports.REQUEST_FILTER = REQUEST_FILTER; exports.REQUEST_ID_GENERATOR = REQUEST_ID_GENERATOR; exports.RegistryLoadingInterceptor = RegistryLoadingInterceptor; exports.RegistryLoadingInterceptorModule = RegistryLoadingInterceptorModule; exports.UrlFragmentIdGenerator = UrlFragmentIdGenerator; exports.defaultProviders = defaultProviders; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=anexia-registry-loading-interceptor.umd.js.map