Created
January 15, 2019 12:57
-
-
Save trinhvanhuy/c8d612b24dc2fa078823f6ed52d7ac7a to your computer and use it in GitHub Desktop.
Spinner - Application
This file contains 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, AfterViewInit } from '@angular/core'; | |
import { Angulartics2Piwik } from 'angulartics2/piwik';$ | |
import { LoaderService } from '@services/loader.service'; | |
import * as fromRoot from '@core/reducers'; | |
import { Store } from '@ngrx/store'; | |
import * as fromGlobal from '@actions/global.action'; | |
import { Subscription } from 'rxjs'; | |
import { OnDestroy } from '@angular/core'; | |
export abstract class SubscribableBase implements OnDestroy { | |
protected _subscription = new Subscription(); | |
public ngOnDestroy() { | |
this._subscription.unsubscribe(); | |
} | |
} | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html' | |
}) | |
export class AppComponent extends SubscribableBase implements OnInit, AfterViewInit { | |
public isShowSpinner = false; | |
timer = null; | |
constructor( | |
public store: Store<fromRoot.AppState> | |
) { | |
super(); | |
} | |
ngOnInit() { | |
} | |
ngAfterViewInit() { | |
// Here I use a timer, to wait for 1s, if there is no more request to the server | |
// I will hide the spinner | |
// This action is a key to avoid the spinner flash following the requests in a stack | |
this._subscription.add( | |
this.store.select(fromRoot.getSpinnerCounter).subscribe(spinnerCounter => { | |
clearTimeout(this.timer); | |
this.timer = setTimeout(() => { | |
this.isShowSpinner = spinnerCounter !== 0; | |
}, spinnerCounter !== 0 ? 0 : 1000); | |
}) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment