import { Injectable } from '@angular/core' ; import { HttpRequest , HttpHandler , HttpEvent , HttpInterceptor } from '@angular/common/http' ; import { Observable } from 'rxjs' ; import { finalize } from 'rxjs/operators' ; import { BlockUIService , BLOCKUI_DEFAULT } from 'ng-block-ui' ; import { BlockUIHttpSettings } from './block-ui-http-settings.service' ; @ Injectable ( ) export class BlockUIInterceptor implements HttpInterceptor { private activeHttpRequests : number ; constructor ( private blockUIService , private blockUIHttpSettings ) { this . activeHttpRequests = 0 ; } intercept ( request , next ) { let active = false ; if ( this . shouldBlock ( request ) ) { this . blockUIService . start ( BLOCKUI_DEFAULT ) ; } return next . handle ( request ) . pipe ( finalize ( ( ) => { if ( this . shouldBlock ( request ) ) { const { blockAllRequestsInProgress } = this . blockUIHttpSettings . settings ; const method = blockAllRequestsInProgress ? 'stop' : 'reset' ; this . blockUIService [ method ] ( BLOCKUI_DEFAULT ) ; } } ) ) ; } shouldBlock ( request ) { const { method , urlWithParams } = request ; const { settings } = this . blockUIHttpSettings ; const requestFilters = settings . requestFilters || [ ] ; return ! requestFilters . some ( ( f ) => { if ( f && f . method && f . url ) { return f . method . toUpperCase ( ) === method && f . url . test ( urlWithParams ) ; } else if ( typeof f === 'function' ) { return f ( request ) ; } return f . test ( urlWithParams ) ; } ) ; } }