Created
March 20, 2021 10:07
-
-
Save muhammadawaisshaikh/578b047ca6734dfcf9c63ce5f92bf7ae to your computer and use it in GitHub Desktop.
Implementing global loader Interceptor for Http Calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { BrowserModule } from '@angular/platform-browser'; | |
| import { NgModule } from '@angular/core'; | |
| import { AppRoutingModule } from './app-routing.module'; | |
| import { HttpClientModule } from '@angular/common/http'; | |
| ... | |
| // for token interceptor | |
| import { HTTP_INTERCEPTORS } from '@angular/common/http'; | |
| import { LoaderInterceptorService } from './core/interceptors/loader-interceptor/loader-interceptor.service'; | |
| ... | |
| @NgModule({ | |
| declarations: [ | |
| ... | |
| ], | |
| imports: [ | |
| ... | |
| ], | |
| providers: [ | |
| { | |
| provide: HTTP_INTERCEPTORS, | |
| useClass: LoaderInterceptorService, | |
| multi: true | |
| } | |
| ], | |
| bootstrap: [AppComponent] | |
| }) | |
| export class AppModule { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core'; | |
| import { | |
| HttpRequest, | |
| HttpHandler, | |
| HttpEvent, | |
| HttpInterceptor, | |
| HttpResponse | |
| } from '@angular/common/http'; | |
| import { Observable } from 'rxjs'; | |
| import { LoaderService } from '../../services/loader/loader.service'; | |
| @Injectable({ | |
| providedIn: 'root' | |
| }) | |
| export class LoaderInterceptorService implements HttpInterceptor { | |
| private requests: HttpRequest<any>[] = []; | |
| constructor( | |
| private loaderService: LoaderService | |
| ) { } | |
| removeRequest(req: HttpRequest<any>) { | |
| const i = this.requests.indexOf(req); | |
| if (i >= 0) { | |
| this.requests.splice(i, 1); | |
| } | |
| this.loaderService.isLoading.next(this.requests.length > 0); | |
| } | |
| intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
| this.requests.push(req); | |
| console.log("No of requests--->" + this.requests.length); | |
| this.loaderService.isLoading.next(true); | |
| return Observable.create(observer => { | |
| const subscription = next.handle(req) | |
| .subscribe( | |
| event => { | |
| if (event instanceof HttpResponse) { | |
| this.removeRequest(req); | |
| observer.next(event); | |
| } | |
| }, | |
| err => { | |
| alert('error' + err); | |
| this.removeRequest(req); | |
| observer.error(err); | |
| }, | |
| () => { | |
| this.removeRequest(req); | |
| observer.complete(); | |
| }); | |
| // remove request from queue when cancelled | |
| return () => { | |
| this.removeRequest(req); | |
| subscription.unsubscribe(); | |
| }; | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div *ngIf="loading"> | |
| <img class="loading all-center" src={{loader}} /> | |
| <div class="loader-bg"></div> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @import '../../../styles.scss'; | |
| .loader-bg{ | |
| background: #ffffff; | |
| opacity: 0.9; | |
| position: fixed; | |
| top: 0; | |
| width: 100%; | |
| height: 100%; | |
| } | |
| .loading { | |
| position: absolute; | |
| z-index: 1; | |
| width: 100px; | |
| height: auto; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Component, OnInit } from '@angular/core'; | |
| import { LoaderService } from '../../core/services/loader/loader.service'; | |
| @Component({ | |
| selector: 'app-loader', | |
| templateUrl: './loader.component.html', | |
| styleUrls: ['./loader.component.scss'] | |
| }) | |
| export class LoaderComponent implements OnInit { | |
| loading: boolean; | |
| loader: any = "../../../../assets/img/loading.gif"; | |
| constructor( | |
| private loaderService: LoaderService | |
| ) { } | |
| ngOnInit(): void { | |
| this.loaderService.isLoading.subscribe((v) => { | |
| console.log(v); | |
| this.loading = v; | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core'; | |
| import { BehaviorSubject } from 'rxjs'; | |
| @Injectable({ | |
| providedIn: 'root' | |
| }) | |
| export class LoaderService { | |
| public isLoading = new BehaviorSubject(false); | |
| constructor() { } | |
| } |
Author
muhammadawaisshaikh
commented
Mar 6, 2024
via email
Let’s connect on a call after 6 hours, is it ok with you?
…--
Regards,
*Muhammad Awais*
P: +923312737076
E: ***@***.***
W: https://bit.ly/muhammadawaisshaikh
Video Intro: https://youtu.be/89t-cRNvfMU
*Linkedin:* www.linkedin.com/in/muhammadawaisshaikh
*Github:* www.github.com/muhammadawaisshaikh
On Wed, 6 Mar 2024 at 5:22 PM, shem itoya ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
I think I've found the reason... the problem was with my use... so I'm
trying to create a situation where, anytime the page loads or I make an Api
call the loading variable is set to true and the loader shows
so on my app.component.ts I did this
import { Component, OnInit } from ***@***.***/core';
import { LoaderInterceptor } from
'./interceptors/loader/loader.interceptor';
@component <https://github.com/component>({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
loading: boolean | undefined;
constructor(private loaderInterceptor: LoaderInterceptor) {}
ngOnInit(): void {
this.loaderInterceptor.isLoading().subscribe(loading => {
this.loading = loading;
});
}
}
the constructor throws that error... so I removed it... I still have no
idea how to achieve my aim
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/muhammadawaisshaikh/578b047ca6734dfcf9c63ce5f92bf7ae#gistcomment-4972814>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AF355Y7IGE4PDUHKAU3CGUTYW4DALBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAOBVGYYTOMJQU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
I'll be having a meeting in the next 6hours, can we make it 7hours?... it's currently 2:30 WAT(west African Time)... so 7hours would be 9:30pm... is that okay?
Hello there, Good Day... I didn't any reply from you yesterday... Is it fine if we reschedule?
Author
What's your email address?
…On Thu, Mar 7, 2024 at 11:49 AM shem itoya ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Hello there, Good Day... I didn't any reply from you yesterday... Is it
fine if we reschedule?
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/muhammadawaisshaikh/578b047ca6734dfcf9c63ce5f92bf7ae#gistcomment-4974633>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AF355Y2FHXLVQBFWSHLWLZDYXAEY5BFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAOBVGYYTOMJQU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
--
--
Regards,
*Muhammad Awais*
P: +923312737076
E: ***@***.***
W: https://bit.ly/muhammadawaisshaikh
Video Intro: https://youtu.be/89t-cRNvfMU
*Linkedin:* www.linkedin.com/in/muhammadawaisshaikh
*Github:* www.github.com/muhammadawaisshaikh
Author
Ok
…--
Regards,
*Muhammad Awais*
P: +923312737076
E: ***@***.***
W: https://bit.ly/muhammadawaisshaikh
Video Intro: https://youtu.be/89t-cRNvfMU
*Linkedin:* www.linkedin.com/in/muhammadawaisshaikh
*Github:* www.github.com/muhammadawaisshaikh
On Fri, 8 Mar 2024 at 1:49 AM, shem itoya ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
***@***.***
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/muhammadawaisshaikh/578b047ca6734dfcf9c63ce5f92bf7ae#gistcomment-4976023>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AF355YYCSOF4BFSANSSKA7DYXDHFRBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAOBVGYYTOMJQU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Author
Check your calendar hope you have received an invite. Thanks
…--
Regards,
*Muhammad Awais*
P: +923312737076
E: ***@***.***
W: https://bit.ly/muhammadawaisshaikh
Video Intro: https://youtu.be/89t-cRNvfMU
*Linkedin:* www.linkedin.com/in/muhammadawaisshaikh
*Github:* www.github.com/muhammadawaisshaikh
On Fri, 8 Mar 2024 at 2:19 AM, Muhammad Awais ***@***.***>
wrote:
Ok
--
Regards,
*Muhammad Awais*
P: +923312737076
E: ***@***.***
W: https://bit.ly/muhammadawaisshaikh
Video Intro: https://youtu.be/89t-cRNvfMU
*Linkedin:* www.linkedin.com/in/muhammadawaisshaikh
*Github:* www.github.com/muhammadawaisshaikh
On Fri, 8 Mar 2024 at 1:49 AM, shem itoya ***@***.***>
wrote:
> ***@***.**** commented on this gist.
> ------------------------------
>
> ***@***.***
>
> —
> Reply to this email directly, view it on GitHub
> <https://gist.github.com/muhammadawaisshaikh/578b047ca6734dfcf9c63ce5f92bf7ae#gistcomment-4976023>
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AF355YYCSOF4BFSANSSKA7DYXDHFRBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAOBVGYYTOMJQU52HE2LHM5SXFJTDOJSWC5DF>
> .
> You are receiving this email because you authored the thread.
>
> Triage notifications on the go with GitHub Mobile for iOS
> <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
> or Android
> <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
> .
>
>
Oh yes I just received it... it's slated for 3pm tomorrow according to my time... I'd be at the office then, my schedules are usually tight during the day and freer at night... do nights work for you? Say between 8 and 9pm?
Author
What pakistan time are you available?
…On Fri, Mar 8, 2024 at 2:52 AM shem itoya ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Oh yes I just received it... it's slated for 3pm tomorrow according to my
time... I'd be at the office then, my schedules are usually tight during
the day and freer at night... do nights work for you? Say between 8 and 9pm?
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/muhammadawaisshaikh/578b047ca6734dfcf9c63ce5f92bf7ae#gistcomment-4976067>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AF355Y2FO3QP4EA2Y3CIC3TYXDOQ7BFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAOBVGYYTOMJQU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
--
--
Regards,
*Muhammad Awais*
P: +923312737076
E: ***@***.***
W: https://bit.ly/muhammadawaisshaikh
Video Intro: https://youtu.be/89t-cRNvfMU
*Linkedin:* www.linkedin.com/in/muhammadawaisshaikh
*Github:* www.github.com/muhammadawaisshaikh
I think that would be 12am... Pakistan time
I would be available all day tomorrow (saturday)... Maybe that would work best?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment